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

📄 drv_uart0.c

📁 Enhanced LPC213x device drivers,tools ADS1.2
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "config_60_61.h"

#include "config_GD60.h"
#include "include_GD60.h"

#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

extern volatile uint8  r_waittingTime0;

volatile uint8  r_BufferFlag0=0;
volatile uint16 r_inp0=0,r_outp0=0,r_count0=0;
volatile uint16 t_inp0=0,t_outp0=0,t_count0=0;

extern 	 uint8 Comm_Src_Addr;

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

void  Uart0Initialize (uint32 baud, UARTMODE set);
void  SetDefaultUart0(void);
void  __irq IRQ_Uart0Serving (void);
void  ReleaseRS485TE0(void);

void  Uart0SendOver(void);
void  Uart0SendChar(char *send_pt,uint8 send_cnt);
void  Uart0SendString(char *send_pt);
void  Uart0SendAddCrc(uint8 send_cnt);
void  Uart0SendEnter(void);

uint16 MB_CRC16Check(uint8 *puchMsg, uint8 usDataLen);
char   _putchar(char ch);
char   _getchar(void);

void  Uart0SendAdd_Crc(uint8 *send_pt,uint8 send_cnt);
/*
*************************************************************************
** 函数名称 :Uart0Initialize()
** 函数功能 :初始化串口:		设置工作模式和波特率。
** 入口参数 :baud				波特率
** 			: set				模式设置(UARTMODE数据结构)
** 出口参数	:返回1表示成功,0表示参数出错。
status 		: ok
*************************************************************************
*/
void Uart0Initialize (uint32 baud, UARTMODE set)
{
uint32 bak;

	IRQDisable();
			
	PINSEL0 = (PINSEL0&0xfffffff0)|0x00000005;
	
	// 参数过滤
	if ((0 == baud) || (baud > 115200))		 return ;
	if ((set.datab < 5) || (set.datab > 8))	 return ;
	if ((0 == set.stopb) || (set.stopb > 2)) return ;
	if (set.parity > 4)						 return ;
	
	// 设置串口波特率
	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中断 */
	VICVectCntl0 = 0x20|INT_UART0;		// UART0分配到IRQ slot0,即最高优先级
	VICVectAddr0 = (uint32)IRQ_Uart0Serving;	// 设置UART0向量地址
	VICIntEnable = 1<<INT_UART0;		// 使能UART0中断
	
	VICVectAddr = 0x00;					// 中断处理结束	
	bak=U0RBR;							// 读取FIFO的数据,并清除中断
	
	IRQEnable();						// 使能IRQ中断	
}

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

	uart0Set.datab  = 8;
	uart0Set.stopb  = 1;
	uart0Set.parity = 0;
	Uart0Initialize(UART0_BAND_RATE, uart0Set);
}
/*
********************************************************************************
** 函数名称 :IRQ_Uart0Serving()
** 函数功能 :串口0接收中断服务程序
** 入口参数 :无
** 出口参数 :无
** status	ok
********************************************************************************
*/
void __irq IRQ_Uart0Serving (void)
{
uint8 buf;
#ifdef	UART_FAST_MODE
uint8 i;
#endif

	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;
				break;
				
////receiving data
			case 0x04:						//RDA interrupt
			case 0x0c:						//CTI interrupt
				buf=U0LSR;					//	

				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++;
					buf=U0LSR;				//more ?

					r_BufferFlag0 = _DATA_OK;
					r_waittingTime0 = 5;
				}
				break;

////sending data
			case 0x02:											//THRE interrupt
				buf=U0LSR;

#ifndef	UART_FAST_MODE
				while(((buf&0x20)==0x20)&&(t_count0!=0)){		//check the THRE empty?
					IO1CLR=RS485TE0_P1o;		//set it sending mode
					U0THR = t_buf0[t_outp0];	// 发送数据 here
					t_outp0++;	if(t_outp0>=TX_BUFFER_LEN)  t_outp0=0;
					t_count0--;
					buf=U0LSR;
				}
#else
				i=0x5a;
				if(((buf&0x20)==0x20)&&(t_count0!=0)){		//check the THRE empty?
					IO1CLR=RS485TE0_P1o;		// set it sending mode
					for(i=0;((i<12)&&(t_count0!=0));i++){			//fifo depth is 16
						U0THR = t_buf0[t_outp0];	// 发送数据 here
						t_outp0++;	if(t_outp0>=TX_BUFFER_LEN)  t_outp0=0;
						t_count0--;
//						if(t_count0==0)	break;
					}
				}
#endif

				if(t_count0==0){
					t_outp0=t_inp0;				//all has been transmited
//					if(i==0x5a){ IO1SET = RS485TE0_P1o; }
				}
				break;

////default condition
			default:
				break;
		
		}
		buf=U0IIR;			//check the other case
	}
	VICVectAddr = 0x00;		// 中断处理结束
}


////////////////////////////////////////////////////////////////////////////////
//sending anything to host without ending flag(such as CRC or ENTER)
//state 	:ok
////////////////////////////////////////////////////////////////////////////////
void ReleaseRS485TE0(void)
{
	if(((IO1PIN&RS485TE0_P1o)==0x0000)&&(t_count0==0)){
		if((U0LSR&0x40)==0x40){					//sending over
			DelayUS_(50);
			IO1SET=RS485TE0_P1o;				//set it receiving mode
		}
	}
}	

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

	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中断 
	IO1CLR=RS485TE0_P1o;		//set it sending mode
	DelayUS_(1000000/UART0_BAND_RATE);

	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((t_count0!=0)||((U0LSR&0x20)==0));	//wait until t_buff empty
		

⌨️ 快捷键说明

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