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

📄 wuart0lib.c

📁 在S3C44b0上移植ucos并实现哲学家就餐问题的演示
💻 C
字号:
/**************************************************************************
NAME:WUART0LIB.C
copyright:wzz at Qingdao University  2008
**************************************************************************/
#include "..\inc\WOPTIONS.h"


#if UART_METHOD==1
/*METHOD:1 FOR UP-NET ARM3000
		 2 FOR FHRF BORAD
		 3WRITEN MY OWN*/
//*************************************************************************
/* flush serial input queue. returns 0 on success or negative error
 * number otherwise
 */
static int s3c44b0_serial0_flush_input(void)
{
	volatile U32 tmp;

	/* keep on reading as long as the receiver is not empty */
	while(rUTRSTAT0&0x01) {
		tmp = rURXH0;
	}
	return 0;
}
//-----------------------------------------------------------------------
/* flush output queue. returns 0 on success or negative error number
 * otherwise
 */
static int s3c44b0_serial0_flush_output(void)
{
	//testuart();
	/* wait until the transmitter is no longer busy */
	while(!(rUTRSTAT0&0x02));
	return 0;
}

//-----------------------------------------------------------------------

/* initialise serial port at the request baudrate. returns 0 on
 * success, or a negative error number otherwise
 */
static int s3c44b0_serial0_init(int baud)
{
	
	int i;

	s3c44b0_serial0_flush_output();
		/*rUFCON0 = 0x0;//FIFO disable
	rULCON0 = 0x03;//Normal,No parity,1 stop,8 bit
	rUCON0 = 0x5;
	rUBRDIV0 = crUBRDIV0;//( (int)(MCLK/16./baud + 0.5) -1 );

	for(i=0; i<100; i++);
	return 0;*/	

	rUFCON0 = crUFCON0_DIS;//FIFO disable
	rULCON0 = crULCON0;//0x03;//Normal,No parity,1 stop,8 bit
	rUCON0 = crUCON0;//0x5;
	rUBRDIV0 = crUBRDIV0;//( (int)(MCLK/16./baud + 0.5) -1 );

	for(i=0; i<100; i++);
	return 0;
}

//-----------------------------------------------------------------------

/* check if there is a character available to read. returns 1 if there
 * is a character available, 0 if not, and negative error number on
 * failure */
static int s3c44b0_serial0_poll(void)
{
	/* check for errors */
	if(rUERSTAT0&0x07)
		return -1;

	return (rUTRSTAT0&0x01);
}


//-----------------------------------------------------------------------

/* read one character from the serial port. return character (between
 * 0 and 255) on success, or negative error number on failure. this
 * function is blocking */
static int s3c44b0_serial0_read(void)
{
	int rv;

	for(;;) {
		rv = s3c44b0_serial0_poll();

		if(rv < 0)
			return rv;

		if(rv > 0)
			return rURXH0;
	}
}


//-----------------------------------------------------------------------

/* write character to serial port. return 0 on success, or negative
 * error number on failure. this function is blocking
 */
static int s3c44b0_serial0_write(int c)
{
	/* wait for room in the transmit FIFO */
	while(!(rUTRSTAT0&0x02));

	rUTXH0 = c;

	return 0;
}


//****************************************************************************


/* export serial driver */
serial_driver_t s3c44b0_serial0_driver = {
	s3c44b0_serial0_init,
	s3c44b0_serial0_read,
	s3c44b0_serial0_write,
	s3c44b0_serial0_poll,
	s3c44b0_serial0_flush_input,
	s3c44b0_serial0_flush_output
};


//*****************************************************************************
//extern serial_driver_t s3c44b0_serial0_driver, s3c44b0_serial1_driver;
serial_driver_t s3c44b0_serial1_driver;

//serial_driver_t* serial_drv=&s3c44b0_serial0_driver;
serial_driver_t* serial_drv[]={&s3c44b0_serial0_driver, &s3c44b0_serial1_driver};
serial_loop_func_t Getch_loopfunc[]={(serial_loop_func_t)NULL,(serial_loop_func_t)NULL,(serial_loop_func_t)NULL, (serial_loop_func_t)NULL};
#define GETCH_LOOPFUNC_NUM		(sizeof(Getch_loopfunc)/sizeof(serial_loop_func_t))

/************************* UART ******************************************/

int Uart_Init(int whichUart, int baud)
//int Uart_Init(int baud)
{
	if(whichUart>=sizeof(serial_drv)/sizeof(serial_driver_t*))
		return FALSE;

	return serial_drv[whichUart]->init(baud);
}


/**************************************************************************
	设置等待串口数据的时候的循环函数,
	成功返回函数的序号(删除的时候使用),
	如果失败则返回-1,
	
*************************************************************************/
int Set_UartLoopFunc(serial_loop_func_t func)
{
	int i;

	for(i=0;Getch_loopfunc[i];i++);

	if(i>=GETCH_LOOPFUNC_NUM)
		return -1;

	Getch_loopfunc[i]=func;
	return i;
}

/**************************************************************************
	清除等待串口数据的时候的循环函数,
	参数是函数的序号,
	成功返回TURE,失败则返回FALSE
	
*************************************************************************/
int Clear_UartLoopFunc(int index)
{
	if(index>=GETCH_LOOPFUNC_NUM || index<0)
		return FALSE;

	Getch_loopfunc[index]=NULL;

	return TRUE;
}

//-----------------------------------------------------------------------
char Uart_Getch(int whichUart)
{
	int i;

	if(whichUart>=sizeof(serial_drv)/sizeof(serial_driver_t*))
		return FALSE;

	while(!serial_drv[whichUart]->poll()){
		for(i=0;i<GETCH_LOOPFUNC_NUM;i++){
			if(Getch_loopfunc[i])
				(*Getch_loopfunc[i])();
		}
	}
	return serial_drv[whichUart]->read();
}
//-----------------------------------------------------------------------
//串口是否有数据输入
int Uart_Poll(int whichUart)
{
	if(whichUart>=sizeof(serial_drv)/sizeof(serial_driver_t*))
		return FALSE;

	return serial_drv[whichUart]->poll();
}
//-----------------------------------------------------------------------
//发送缓冲区清空
void Uart_TxEmpty(int whichUart)
{
	if(whichUart<sizeof(serial_drv)/sizeof(serial_driver_t*))
		serial_drv[whichUart]->flush_output();
}
//-----------------------------------------------------------------------
//接收缓冲区清空
void Uart_RxEmpty(int whichUart)
{
	if(whichUart<sizeof(serial_drv)/sizeof(serial_driver_t*))
		serial_drv[whichUart]->flush_input();
}
//-----------------------------------------------------------------------
int Uart_SendByte(int whichUart,int data)
{
	if(whichUart>=sizeof(serial_drv)/sizeof(serial_driver_t*))
		return FALSE;

	return serial_drv[whichUart]->write(data);
}		
//-----------------------------------------------------------------------
void Uart_GetString(char *string)
{
    char *string2=string;
    char c;
    while((c=Uart_Getch(0))!='\r')
    {
		if(c=='\b')
		{
		    if(	(int)string2 < (int)string )
		    {
				Uart_Printf("\b \b");
				string--;
		    }
		}
		else 
		{
		    *string++=c;
		    Uart_SendByte(0,c);
		}
    }
    *string='\0';
    Uart_SendByte(0,'\r');
    Uart_SendByte(0,'\n');
}

//-----------------------------------------------------------------------
int Uart_GetIntNum(void)
{
    char str[30];
    char *string=str;
    int base=10;
    int minus=0;
    int lastIndex;
    int result=0;
    int i;
    
    Uart_GetString(string);
    
    if(string[0]=='-')
    {
        minus=1;
        string++;
    }
    
    if(string[0]=='0' && (string[1]=='x' || string[1]=='X'))
    {
		base=16;
		string+=2;
    }
    
    lastIndex=strlen(string)-1;
    if( string[lastIndex]=='h' || string[lastIndex]=='H' )
    {
		base=16;
		string[lastIndex]=0;
		lastIndex--;
    }

    if(base==10)
    {
		result=atoi(string);
		result=minus ? (-1*result):result;
    }
    else
    {
		for(i=0;i<=lastIndex;i++)
		{
    	    if(isalpha(string[i]))
			{
				if(isupper(string[i]))
					result=(result<<4)+string[i]-'A'+10;
				else
				    result=(result<<4)+string[i]-'a'+10;
			}
			else
			{
				result=(result<<4)+string[i]-'0';
			}
		}
		result=minus ? (-1*result):result;
    }
    return result;
}

//-----------------------------------------------------------------------
void Uart_SendString(char *pt)
{
    while(*pt){

	if(*pt=='\n')
		Uart_SendByte(0,'\r');

	Uart_SendByte(0,*pt++);
    }
}
//-----------------------------------------------------------------------

//if you don't use vsprintf(), the code size is reduced very much.
void Uart_Printf(char *fmt,...)
{
	va_list ap;
	static char string[256];

	va_start(ap,fmt);
	vsprintf(string,fmt,ap);
	Uart_SendString(string);
	va_end(ap);
}

#endif
#if UART_METHOD==2
/************************* UART2 *********************************************************/
static int whichUart=0;

void Uart_Init(int mclk,int baud)
{
    int i;
    if(mclk==0)
    mclk=MCLK;
    rUFCON0=0x0;     //FIFO disable
    rUFCON1=0x0;
    rUMCON0=0x0;
    rUMCON1=0x0;
//UART0
    rULCON0=0x3;     //Normal,No parity,1 stop,8 bit
//    rULCON0=0x7;     //Normal,No parity,2 stop,8 bit
    rUCON0=0x245;    //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
    rUBRDIV0=( (int)(mclk/16./baud + 0.5) -1 );
//UART1
//    rULCON1=0x7;     //Normal,No parity,2 stop,8 bit
//    rULCON1=0x3;
//    rUCON1=0x245;
//    rUBRDIV1=( (int)(mclk/16./baud + 0.5) -1 );

    for(i=0;i<100;i++);
}

//-----------------------------------------------------------------------
void Uart_Select(int ch)
{
    whichUart=ch;
}

//-----------------------------------------------------------------------
void Uart_TxEmpty(int ch)
{
    if(ch==0)
	while(!(rUTRSTAT0 & 0x4)); //wait until tx shifter is empty.
    else
    	while(!(rUTRSTAT1 & 0x4)); //wait until tx shifter is empty.
}

//-----------------------------------------------------------------------
char Uart_Getch(void)
{
    if(whichUart==0)
    {	    
	while(!(rUTRSTAT0 & 0x1)); //Receive data read
	return RdURXH0();
    }
    else
    {
	while(!(rUTRSTAT1 & 0x1)); //Receive data ready
	return	rURXH1;
    }
}

//-----------------------------------------------------------------------
char Uart_GetKey(void)
{
    if(whichUart==0)
    {	    
	if(rUTRSTAT0 & 0x1)    //Receive data ready
    	    return RdURXH0();
	else
	    return 0;
    }
    else
    {
	if(rUTRSTAT1 & 0x1)    //Receive data ready
	    return rURXH1;
	else
	    return 0;
    }
}

//-----------------------------------------------------------------------
void Uart_GetString(char *string)
{
    char *string2=string;
    char c;
    while((c=Uart_Getch())!='\r')
    {
	if(c=='\b')
	{
	    if(	(int)string2 < (int)string )
	    {
		Uart_Printf("\b \b");
		string--;
	    }
	}
	else 
	{
	    *string++=c;
	    Uart_SendByte(c);
	}
    }
    *string='\0';
    Uart_SendByte('\n');
}

//-----------------------------------------------------------------------
int Uart_GetIntNum(void)
{
    char str[30];
    char *string=str;
    int base=10;
    int minus=0;
    int lastIndex;
    int result=0;
    int i;
    
    Uart_GetString(string);
    
    if(string[0]=='-')
    {
        minus=1;
        string++;
    }
    
    if(string[0]=='0' && (string[1]=='x' || string[1]=='X'))
    {
	base=16;
	string+=2;
    }
    
    lastIndex=strlen(string)-1;
    if( string[lastIndex]=='h' || string[lastIndex]=='H' )
    {
	base=16;
	string[lastIndex]=0;
	lastIndex--;
    }

    if(base==10)
    {
	result=atoi(string);
	result=minus ? (-1*result):result;
    }
    else
    {
	for(i=0;i<=lastIndex;i++)
	{
    	    if(isalpha(string[i]))
	    {
		if(isupper(string[i]))
		    result=(result<<4)+string[i]-'A'+10;
		else
		    result=(result<<4)+string[i]-'a'+10;
	    }
	    else
	    {
		result=(result<<4)+string[i]-'0';
	    }
	}
	result=minus ? (-1*result):result;
    }
    return result;
}

//-----------------------------------------------------------------------
void Uart_SendByte(int data)
{
    if(whichUart==0)
    {
	if(data=='\n')
	{
	    while(!(rUTRSTAT0 & 0x2));
	    Delay(10);	//because the slow response of hyper_terminal 
	    WrUTXH0('\r');
	}
	while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
	Delay(10);
	WrUTXH0(data);
    }
    else
    {
	if(data=='\n')
	{
    	    while(!(rUTRSTAT1 & 0x2));
	    Delay(10);	//because the slow response of hyper_terminal 
	    rUTXH1='\r';
	}
	while(!(rUTRSTAT1 & 0x2));  //Wait until THR is empty.
	Delay(10);
	rUTXH1=data;
    }	
}		

//-----------------------------------------------------------------------
void Uart_SendString(char *pt)
{
    while(*pt)
	Uart_SendByte(*pt++);
}

//-----------------------------------------------------------------------
//if you don't use vsprintf(), the code size is reduced very much.
void Uart_Printf(char *fmt,...)
{
    va_list ap;
    char string[256];

    va_start(ap,fmt);
    vsprintf(string,fmt,ap);
    Uart_SendString(string);
    va_end(ap);
}
//*************************************************************************

//*************************************************************************
#endif

⌨️ 快捷键说明

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