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

📄 drv_uart0.c

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

volatile uint8 	r_buf0[RX_BUFFER_LEN];	// UART0 receiving buffer
volatile uint8 	t_buf0[TX_BUFFER_LEN];	// UART0 sending buffer

volatile uint8  r_ActiveFlag0=0;
volatile uint16 r_inp0=0,r_outp0=0,r_count0=0;
volatile uint16 t_inp0=0,t_outp0=0,t_count0=0;
extern volatile uint16 mSecondTick;		// for delay function

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

uint8 Uart0Initialize (uint32 baud, UARTMODE set);
void SetDefaultUart0(void);
void __irq IRQ_Uart0Serving (void);
void Uart0SendingOver(void);
void Uart0SendingChar(char *send_pt,uint8 send_cnt);
void Uart0SendString(char *send_pt);
char _putchar(char ch);
char _getchar(void);
void Uart0SendAddCrc(uint8 send_cnt);
void Uart0SendEnter(void);
uint16 Uart0CheckCRC16(uint8 *puchMsg, uint8 usDataLen);

/*
*************************************************************************
** 函数名称 :Uart0Initialize()
** 函数功能 :初始化串口:		设置工作模式和波特率。
** 入口参数 :baud				波特率
** 			: set				模式设置(UARTMODE数据结构)
** 出口参数	:返回1表示成功,0表示参数出错。
status 		: ok
*************************************************************************
*/
uint8 Uart0Initialize (uint32 baud, UARTMODE set)
{
	uint32 bak;
	
	PINSEL0 = (PINSEL0&0xfffffff0)|0x00000005;
	
	// 参数过滤
	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);
	
	// 设置串口波特率
	U0LCR = 0x80;		// DLAB=1
	bak = (LPC_FPCLK >> 4) / baud;
	U0DLM = bak >> 8;
	U0DLL = 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;
	U0LCR = bak;

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

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

/*****************************************************************************/
void SetDefaultUart0(void)
{
UARTMODE uart0Set;

	uart0Set.datab = 8;
	uart0Set.stopb = 1;
	uart0Set.parity = 0;
	Uart0Initialize(DEFAULT_BAND_RATE, uart0Set);
}
/*
********************************************************************************
** 函数名称 :IRQ_Uart0Serving()
** 函数功能 :串口0接收中断服务程序
** 入口参数 :无
** 出口参数 :无
** status	ok
********************************************************************************
*/
void __irq IRQ_Uart0Serving(void)
{
uint8 buf;
/////
	buf=U0IIR;								//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=U0LSR;
//				if(buf&0x02) 	Uart0SendString("(11) Over Run Error");
//				if(buf&0x04) 	Uart0SendString("(12) Parity   Error");
//				if(buf&0x08) 	Uart0SendString("(13) Frame    Error");
//				if(buf&0x10) 	Uart0SendString("(14) Break    Error");
//				if(buf&0x80) 	Uart0SendString("(15) Rx FIFO  Error");
				break;
				
////receiving data
			case 0x04:						//RDA interrupt
			case 0x0c:						//CTI interrupt
				buf=U0LSR;					//	
//				if(buf&0x02) 	Uart0SendString("(16) Over Run Error");
//				if(buf&0x04) 	Uart0SendString("(17) Parity   Error");
//				if(buf&0x08) 	Uart0SendString("(18) Frame    Error");
//				if(buf&0x10) 	Uart0SendString("(19) Break    Error");
//				if(buf&0x80) 	Uart0SendString("(1A) Rx FIFO  Error");

				while((buf&0x01)!=0x0){		//uxlsr_bit0 indicates data in uxrbr available
					if(r_count0==0)	r_outp0=r_inp0;
					r_buf0[r_inp0]=U0RBR;		//读取FIFO的数据,并清除中断
					r_inp0++;	if(r_inp0>=RX_BUFFER_LEN)  r_inp0=0;
					r_count0++;
					r_ActiveFlag0 = _DATA_AVAILABLE;
					
					buf=U0LSR;				//more ?
//					if(buf&0x02) 	Uart0SendString("(1B) Over Run Error");
//					if(buf&0x04) 	Uart0SendString("(1C) Parity   Error");
//					if(buf&0x08) 	Uart0SendString("(1D) Frame    Error");
//					if(buf&0x10) 	Uart0SendString("(1E) Break    Error");
//					if(buf&0x80) 	Uart0SendString("(1F) Rx FIFO  Error");
				}
				break;

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

					IO1CLR = LED3;			// LED1点亮

					U0THR = t_buf0[t_outp0];	// 发送数据 here
					t_outp0++;	if(t_outp0>=TX_BUFFER_LEN)  t_outp0=0;
					t_count0--;
					buf=U0LSR;
				}
				if(t_count0==0){
					t_outp0=t_inp0;			//all has been transmited

					IO1SET = LED3;			// LED1熄灭

				}
				break;

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

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

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

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

	IRQEnable();				// 使能IRQ中断 
		
	U0THR = t_buf0[t_outp0];		//initiate sending
	t_outp0++;	if(t_outp0>=TX_BUFFER_LEN)  t_outp0=0;
	t_count0--;
}

//////////////////////////////////////////////////////////////////////////////
//sending string ending with ENTER to computer or host 
//state	:ok
//////////////////////////////////////////////////////////////////////////////
void Uart0SendString(char *send_pt)
{
	
//	while((U0LSR&0x20)==0);				// 等待数据发送完毕
	while((t_count0!=0)||((U0LSR&0x20)==0));	//wait until t_buff empty
	
	IRQDisable();
		
	do
	{
			t_buf0[t_inp0]=*send_pt;
			t_inp0++;	if(t_inp0>=TX_BUFFER_LEN)  t_inp0=0;	//elarge the sending buffer

			send_pt++;
			t_count0++;

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

	t_buf0[t_inp0]=0x0a;				
	t_inp0++;	if(t_inp0>=TX_BUFFER_LEN)  	t_inp0=0;
	t_count0++;

	IRQEnable();				// 使能IRQ中断 
		
	U0THR = t_buf0[t_outp0];	//initiate sending
	t_outp0++;	if(t_outp0>=TX_BUFFER_LEN)  	t_outp0=0;
	t_count0--;

}

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

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

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

		IRQDisable();

		crc_data=Uart0CheckCRC16((uint8*)t_buf0,send_cnt);			//attached CRC code
		t_buf0[send_cnt]	  =(unsigned char)(crc_data>>8);
		t_buf0[send_cnt+1] =(unsigned char) crc_data;
	
		t_count0=send_cnt+2;
		t_outp0=0;

		IRQEnable();				// 使能IRQ中断 
			
		U0THR = t_buf0[0];			//initiate sending
		t_outp0++;
		t_count0--;

//for ModBus
//	}
//	else{
//	t_count0=0;
//	t_outp0=t_inp0=0;					//all has been transmited
//	}

}

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

	IRQDisable();
	
	t_buf0[t_inp0]=0x0d;
	t_inp0++;	if(t_inp0>=TX_BUFFER_LEN)  	t_inp0=0;
	t_count0++;

	t_buf0[t_inp0]=0x0a;				
	t_inp0++;	if(t_inp0>=TX_BUFFER_LEN)  	t_inp0=0;
	t_count0++;

	IRQEnable();				// 使能IRQ中断 
		
	U0THR = t_buf0[t_outp0];		//initiate sending
	t_outp0++;	if(t_outp0>=TX_BUFFER_LEN)  	t_outp0=0;
	t_count0--;

}

//////////////////////////////////////////////////////////////////////
// Simple implementation of the ANSI putchar() routine, writes to UART
char _putchar(char ch)
{
//	while((U0LSR&0x20)==0);				// 等待数据发送完毕
	while((t_count0!=0)||((U0LSR&0x20)==0));						// wait until t_buff empty

	IRQDisable();
	
	t_buf0[t_inp0]=ch;
	t_inp0++;	if(t_inp0>=TX_BUFFER_LEN)  	t_inp0=0;		//elarge the sending buffer
	t_count0++;
	
	IRQEnable();				// 使能IRQ中断 
		
	U0THR = t_buf0[t_outp0];					//initiate sending
	t_outp0++;	if(t_outp0>=TX_BUFFER_LEN)  	t_outp0=0;
	t_count0--;
	
  	return ch;
}

///////////////////////////////////////////////////////////////////////
// Simple implementation of the ANSI getchar() routine, reads from UART
char _getchar(void)
{
char charbuff;
uint16 r_count_old;
uint16 r_inp_;

	r_count_old=r_count0;
	mSecondTick=5000;
	while(r_count_old==r_count0){  /* Poll the serial port until a character is available*/
		WatchDogClear();
		if(mSecondTick==0) break;
	}
	
	if(r_inp0==0){
		r_inp_= RX_BUFFER_LEN-1;
	}else{
		r_inp_=r_inp0-1;
	}

	charbuff=r_buf0[r_inp_];

  	return charbuff;   				/* Return received character */
}

////////////////////////////////////////////////////////////
//            calculate CRC_16 check coed
/* CRC 高位字节值表 */ 
unsigned char auchCRCHi[256] = { 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 }; 

/* CRC低位字节值表*/ 
unsigned char auchCRCLo[256] = { 
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 
0x1E, 0xDE, 0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 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 
0x43, 0x83, 0x41, 0x81, 0x80, 0x40 };

//////////////////////////////////////////////////////////////////////
/* 要进行CRC校验的消息 ,消息中字节数 */
uint16 Uart0CheckCRC16(uint8 *puchMsg, uint8 usDataLen) 
{ 
   unsigned char uchCRCHi = 0xFF ; 	/* 高CRC字节初始化 */ 
   unsigned char uchCRCLo = 0xFF ; 	/* 低CRC 字节初始化 */ 
   unsigned char uIndex ; 			/* CRC循环中的索引 */ 

   while(usDataLen--) 			/* 传输消息缓冲区 */ 
   { 
      uIndex = uchCRCHi ^ *puchMsg++ ; 	/* 计算CRC */ 
      uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ; 
      uchCRCLo = auchCRCLo[uIndex] ; 
   } 
   return (unsigned short)(uchCRCHi << 8 | uchCRCLo) ; 
}
//////////////////////// end of file ///////////////////////////////

⌨️ 快捷键说明

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