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

📄 drv_uart1.c

📁 CC1000 Rf modem C codes for philips sLpc213X ARM MCU.
💻 C
字号:
#include "config_GD61.h"
#include "include_GD61.h"

volatile uint8 r_buf1[RX_BUFFER_LEN];		// UART1 receiving buffer
volatile uint8 t_buf1[TX_BUFFER_LEN];		// UART1 sending buffer

volatile uint16  r_inp1=0,r_outp1=0,r_count1=0;
volatile uint16  t_inp1=0,t_outp1=0,t_count1=0;

////////////////////////////////////////////////////////////////////////

uint8 Uart1Initialize (uint32 baud, UARTMODE set);
void SetDefaultUart1(void);
void __irq IRQ_Uart1Serving (void);
void Uart1SendOver(void);
void Uart1SendChar(char *send_pt,uint8 send_cnt);
void Uart1SendString(char *send_pt);
void Uart1SendAddCrc(unsigned char send_cnt);
void Uart1SendEnter(void);

/*
*************************************************************************
** 函数名称 :Uart0Initialize()
** 函数功能 :初始化串口:		设置工作模式和波特率。
** 入口参数 :baud				波特率
** 			: set				模式设置(UARTMODE数据结构)
** 出口参数	:返回1表示成功,0表示参数出错。
status 		: ok
*************************************************************************
*/
uint8 Uart1Initialize (uint32 baud, UARTMODE set)
{
uint32 bak;
	
	PINSEL0 = (PINSEL0 & (~(0x0F<<16))) | (0x05<<16);	// 设置I/O连接到UART1

	// 参数过滤
	if ((0 == baud)      || (baud > 115200)) return (0);
	if ((set.datab < 5)  || (set.datab > 8)) return (0);
	if ((0 == set.stopb) || (set.stopb > 2)) return (0);
	if (set.parity > 4)			 return (0);
	
	// 设置串口波特率
	U1LCR = 0x80;						// DLAB=1
	bak   = (LPC_FPCLK >> 4) / baud;
	U1DLM = bak  >> 8;
	U1DLL = bak & 0xff;
	
	// 设置串口模式
	bak  = set.datab - 5;
	if (2 == set.stopb)	bak |= 0x04;
	
	if (0 != set.parity)
	{
		set.parity = set.parity - 1;
		bak |= 0x08;
	}
	bak |= set.parity << 4;
	U1LCR = bak;

	U1FCR = 0x81;						// 使能FIFO,并设置触发点为8字节
	U1IER = 0x03;						// 允许UART中断, receive and sending int

	/* 使能UART1中断 */
//	VICIntSelect = 0x00000000;			// 设置所有的通道为IRQ中断
	VICVectCntl1 = 0x20|INT_UART1;		// UART1分配到IRQ slot1,即最高优先级
	VICVectAddr1 = (uint32)IRQ_Uart1Serving;	// 设置UART1向量地址
	VICIntEnable = 1<<INT_UART1;		// 使能UART1中断
		
	VICVectAddr = 0x00;			// 中断处理结束	
	bak=U1RBR;					//读取FIFO的数据,并清除中断
	
	return (0);
}

/*****************************************************************************/
void SetDefaultUart1(void)
{
UARTMODE uart1Set;

	uart1Set.datab = 8;
	uart1Set.stopb = 1;
	uart1Set.parity = 0;
	Uart1Initialize(DEFAULT_BAND_RATE, uart1Set);
}	
/*
********************************************************************************
** 函数名称 :IRQ_Uart0Serving()
** 函数功能 :串口0接收中断服务程序
** 入口参数 :无
** 出口参数 :无
** status	ok
********************************************************************************
*/
void __irq IRQ_Uart1Serving(void)
{
uint8 buf;
////
	TraceFunAddr((void*)IRQ_Uart1Serving);	//for watchdog tracing
////	
	buf=U1IIR;								//read interrupt identification register
	while((buf&0x01)==0x0){					//bit0=0 indicates that an interrupt occur
		buf&=0x0f;							//get u0iir[3:0]
		switch(buf){

////line error
			case 0x06:						//RLS interrupt, line error occur
				buf=U1LSR;
//				if(buf&0x02) 	Uart1SendString("(11) Over Run Error");
//				if(buf&0x04) 	Uart1SendString("(12) Parity   Error");
//				if(buf&0x08) 	Uart1SendString("(13) Frame    Error");
//				if(buf&0x10) 	Uart1SendString("(14) Break    Error");
//				if(buf&0x80) 	Uart1SendString("(15) Rx FIFO  Error");
				break;
				
////receiving data
			case 0x04:						//RDA interrupt
			case 0x0c:						//CTI interrupt
				buf=U1LSR;					//	
//				if(buf&0x02) 	Uart1SendString("(16) Over Run Error");
//				if(buf&0x04) 	Uart1SendString("(17) Parity   Error");
//				if(buf&0x08) 	Uart1SendString("(18) Frame    Error");
//				if(buf&0x10) 	Uart1SendString("(19) Break    Error");
//				if(buf&0x80) 	Uart1SendString("(1A) Rx FIFO  Error");

				while((buf&0x01)!=0x0){		//uxlsr_bit0 indicates data in uxrbr available
					if(r_count1==0)	r_outp1=r_inp1;
					r_buf1[r_inp1]=U1RBR;	//读取FIFO的数据,并清除中断
					r_inp1++;	if(r_inp1>=RX_BUFFER_LEN)  r_inp1=0;
					r_count1++;
					
						
					buf=U1LSR;				//more ?
//				if(buf&0x02) 	Uart1SendString("(1B) Over Run Error");
//				if(buf&0x04) 	Uart1SendString("(1C) Parity   Error");
//				if(buf&0x08) 	Uart1SendString("(1D) Frame    Error");
//				if(buf&0x10) 	Uart1SendString("(1E) Break    Error");
//				if(buf&0x80) 	Uart1SendString("(1F) Rx FIFO  Error");
				}
				break;

////sending data
			case 0x02:											//THRE interrupt
				buf=U1LSR;
				while(((buf&0x20)==0x20)&&(t_count1!=0)){		//check the THRE empty?



						U1THR = t_buf1[t_outp1];				// 发送数据 here
						t_outp1++;	if(t_outp1>=TX_BUFFER_LEN)  t_outp1=0;
						t_count1--;
						buf=U1LSR;
				}
				if(t_count1==0){
					t_outp1=t_inp1;					//all has been transmited



				}
				break;

////default condition
			default:
//				Uart1SendString("(10) Unknown Uart1 Action");
				break;
		
		}
		buf=U1IIR;				//check the other case
	}
	
	VICVectAddr = 0x00;			// 中断处理结束
}

////////////////////////////////////////////////////////////////////////////////
//sending anything to host without ending flag(such as CRC or ENTER)
//state 	:ok
////////////////////////////////////////////////////////////////////////////////
void Uart1SendOver(void)
{
//	while(t_count1!=0);							//wait until t_buff empty
	while(((U1LSR&0x20)==0)||(t_count1!=0));	// 等待数据发送完毕
}	
	
////////////////////////////////////////////////////////////////////////////////
//sending anything to host without ending flag(such as CRC or ENTER)
//state 	:ok
////////////////////////////////////////////////////////////////////////////////
void Uart1SendChar(char *send_pt,uint8 send_cnt)
{
//	while((U1LSR&0x20)==0);						// 等待数据发送完毕
	while((t_count1!=0)||((U1LSR&0x20)==0));	//wait until t_buff empty

	IRQDisable();
		
	do
	{
			t_buf1[t_inp1]=*send_pt;
			t_inp1++;	if(t_inp1>=TX_BUFFER_LEN)  	t_inp1=0;		//elarge the sending buffer

			send_pt++;
			t_count1++;
			send_cnt--;
			
	}while(send_cnt>0);							// copy the string to t_buffer

	IRQEnable();				// 使能IRQ中断 

	U1THR = t_buf1[t_outp1];						//initiate sending
	t_outp1++;			if(t_outp1>=TX_BUFFER_LEN)  t_outp1=0;
	t_count1--;
}

//////////////////////////////////////////////////////////////////////////////
//sending string ending with ENTER to computer or host 
//state	:ok
//////////////////////////////////////////////////////////////////////////////
void Uart1SendString(char *send_pt)
{
//	while((U1LSR&0x20)==0);						// 等待数据发送完毕
	while((t_count1!=0)||((U1LSR&0x20)==0));	//wait until t_buff empty

	IRQDisable();
		
	do
	{
			t_buf1[t_inp1]=*send_pt;
			t_inp1++;	if(t_inp1>=TX_BUFFER_LEN)  	t_inp1=0;	//elarge the sending buffer

			send_pt++;
			t_count1++;

	}while(*send_pt!=0);					// copy the string to t_buffer
	
	t_buf1[t_inp1]=0x0d;
	t_inp1++;		if(t_inp1>=TX_BUFFER_LEN)  		t_inp1=0;			//elarge the sending buffer
	t_count1++;

	t_buf1[t_inp1]=0x0a;				
	t_inp1++;		if(t_inp1>=TX_BUFFER_LEN)  		t_inp1=0;
	t_count1++;

	IRQEnable();				// 使能IRQ中断 
	
	U1THR = t_buf1[t_outp1];					//initiate sending
	t_outp1++;		if(t_outp1>=TX_BUFFER_LEN)  	t_outp1=0;
	t_count1--;
}

////////////////////////////////////////////////////////////////////////////
//sending command package from t_buf1[0] to t_buf1[send_cnt+2] including CRC
//status :ok
/**************************************************************************/
void Uart1SendAddCrc(unsigned char send_cnt)
{
unsigned short crc_data;

//for ModBus
//	if( Response_ADDR!=RF_BROAD_CAST_ADDR){		//if broadcast address, don't response

//		while(t_count1!=0);							//wait until t_buff empty
		while(((U1LSR&0x20)==0)||(t_count1!=0));	// 等待数据发送完毕

		IRQDisable();
		
		crc_data=Uart0CheckCRC16((uint8*)t_buf1,send_cnt);	//attached CRC code
		t_buf1[send_cnt]  =(unsigned char)(crc_data>>8);
		t_buf1[send_cnt+1] =(unsigned char) crc_data;
	
		t_count1=send_cnt+2;
		t_outp1=0;
	
		IRQEnable();				// 使能IRQ中断 
	
		U1THR = t_buf1[0];			//initiate sending
		t_outp1++;
		t_count1--;

//for ModBus
//	}
//	else{
//	t_count1=0;
//	t_outp1=t_inp1=0;				//all has been transmited
//	}

}

////////////////////////////////////////////////////////////////////////////
//send ENTER to host
//status: ok
///////////////////////////////////////////////////////////////////////////
void Uart1SendEnter(void)
{
//	while((U1LSR&0x20)==0);						// 等待数据发送完毕
	while((t_count1!=0)||((U1LSR&0x20)==0));	//wait until t_buff empty

	IRQDisable();
	
	t_buf1[t_inp1]=0x0d;
	t_inp1++;	if(t_inp1>=TX_BUFFER_LEN)  t_inp1=0;
	t_count1++;

	t_buf1[t_inp1]=0x0a;				
	t_inp1++;	if(t_inp1>=TX_BUFFER_LEN)  t_inp1=0;
	t_count1++;

	IRQEnable();				// 使能IRQ中断 

	U1THR = t_buf1[t_outp1];					//initiate sending
	t_outp1++;	if(t_outp1>=TX_BUFFER_LEN)  t_outp1=0;
	t_count1--;
}

/***************************************************************************
**                            End Of File
***************************************************************************/

⌨️ 快捷键说明

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