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

📄 lib.c

📁 usoc在北京博创兴业有限公司的实验平台s3c2410上运行。 2. 各实验的全部源代码分别存放在各实验目录下面。
💻 C
字号:
/***************************************************************************\
	Copyright (c) 2004-2007 threewater@up-tech.com, All rights reserved.
	by threewter	2004.4.26
\***************************************************************************/
	

/***************************************************************************\
    #说明: C库函数等定义
	----------------------------------  Bug  --------------------------------------

	----------------------------------  TODO list  --------------------------------------

	----------------------------------修正--------------------------------------
	2004-5-2	1、添加udelay,微秒级延时函数

	2004-4-26	创建

\***************************************************************************/

#include "../inc/lib.h"

#include "../inc/macro.h"
#include "../inc/Uart.h"
#include "../inc/reg2410.h"

#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

extern unsigned int PCLK;
//--------------------------------SYSTEM---------------------------------//

/*************************************************************\
	100微秒级延时函数
	参数time: 表示延时的时间,单位微秒。
	如果time=0,则启用看门狗定时器,
	自动计算时间
\**************************************************************/
void hudelay(int time)
{
	static int delayLoopCount=100;
	int i,adjust=0, curpc;

	if(time==0)
	{
		time=adjust=200;
		delayLoopCount = 400;
		//PCLK/1M,Watch-dog disable,1/64,interrupt disable,reset disable
		rWTCON = ((PCLK/1000000-1)<<8)|(2<<3); 
		rWTDAT = 0xffff;                              //for first update
		rWTCNT = 0xffff;                              //resolution=64us @any PCLK 
		rWTCON = ((PCLK/1000000-1)<<8)|(2<<3)|(1<<5); //Watch-dog timer start
	}

	for(;time>0;time--){
		__asm{mov curpc, pc};

		for(i=0;i<delayLoopCount;i++);

		__asm{sub curpc, pc, curpc};
	}

	if(adjust)
	{
		rWTCON = ((PCLK/1000000-1)<<8)|(2<<3);   //Watch-dog timer stop
		i = 0xffff - rWTCNT;                     // 1count->64us, 200*400 cycle runtime = 64*i us
		delayLoopCount = 8000000/(i*64);         //200*400:64*i=1*x:100 -> x=80000*100/(64*i)   

		printk("Get delay loop count successfully! The value is %d, ", delayLoopCount);
		printk("Maybe %d MIPS\n", curpc*delayLoopCount/400);
	}
}

/************************* SYSTEM PARAMETER ***************************/
//struct struct_system_param system_param;



/************************* UART ****************************/
extern serial_driver_t* serial_drv[];
extern int NumberOfUartDrv;

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))

int Uart_Init(int whichUart, int baud)
{
	if(whichUart>=NumberOfUartDrv)
		return FALSE;

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

int Uart_Init_Extend(int whichUart, int baud)
{//Uart_Printf(0,"\n\nenter uart_init_extend");
 //LCR|=0x83;
 //hudelay(10);
 //Uart_Printf(0,"\nDLM=%X in Uart_Init_Extend",DLM);
 //Uart_Printf(0,"\nDLL=%X in Uart_Init_Extend",DLL);
 LCR=0;
 //Uart_Printf(0,"\nLCR=%X in Uart_Init_Extend",LCR);
 IER=0;
 FCR_ISR=0;
 MCR=0x3;//正常模式
 //MCR=0;
 //MCR|=(1<<4)|(0x3);//回环模式
 //Uart_Printf(0,"\nMCR=%X in Uart_Init_Extend",MCR);
 LCR|=0x83;
 hudelay(10);
 //Uart_Printf(0,"\nLCR=%X in Uart_Init_Extend",LCR);
 //DLM=0x0;
 DLL=0x6;
 DLM=0;
 //Uart_Printf(0,"\nDLM=%X in Uart_Init_Extend",DLM);
 //Uart_Printf(0,"\nDLL=%X in Uart_Init_Extend",DLL);
 LCR&=0x7f;
 //Uart_Printf(0,"\nLCR=%X in Uart_Init_Extend",LCR);
 //Uart_Printf(0,"\nquit uart_init_extend");
}


int Uart_SendByte(int whichUart,int data)
{
	if(whichUart>=NumberOfUartDrv)
		return FALSE;

	return serial_drv[whichUart]->write(data);
}

void Uart_SendString(int whichUart, char *pt)
{
    while(*pt){

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

	Uart_SendByte(whichUart,*pt++);
    }
}

void Uart_Printf(int whichUart, char *fmt,...)
{
	va_list ap;
	static char string[256];

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

char Uart_Getch(char* Revdata, int Uartnum, int timeout)
{
	int i=0;
	if(Uartnum==0){
		while(!(rUTRSTAT0 & 0x1)); //Receive data read
		*Revdata=RdURXH0();
		return TRUE;
	}
	else if(Uartnum==1){
		while(!(rUTRSTAT1 & 0x1));//Receive data read
		*Revdata=RdURXH1();
		return TRUE;
	}
	else{
		//while(!(rUTRSTAT2 & 0x1));//Receive data read
		/**Revdata=RdURXH2();
		return TRUE;*/
		//Uart_Printf(0,"LSR=%X",LSR);
		while(!(LSR&0x1));
		*Revdata=(U8)THR_RHR;
		return TRUE;
	}
}
//
void Uart_SendByten(int Uartnum, char data)//ok eric rong
{   //int i;
	if(Uartnum==0)
    {
		while(!(rUTRSTAT0 & 0x4)); //Wait until THR is empty.
		
		hudelay(10);
		WrUTXH0(data);
   	}
	else if(Uartnum==1)
    {
		while(!(rUTRSTAT1 & 0x4));  //Wait until THR is empty.
		
		hudelay(10);
		WrUTXH1(data);
    }
    else
    {
		/*while(!(rUTRSTAT2 & 0x4));  //Wait until THR is empty.
		
		hudelay(10);
		WrUTXH2(data);*/
		//Uart_Printf(0,"\nLCR=%x in Uart_SendByten",LCR);
		//Uart_Printf(0,"\nLSR=%x in Uart_SendByten",LSR);
		while(!(LSR&0x40));
		THR_RHR=data;
		
    }	
}	

  /* S16 Uart_Getchn(char* Revdata, int Uartnum, int timeout)
{
	
	if(Uartnum==0){
		while(!(rUTRSTAT0 & 0x1)); //Receive data read
		*Revdata=RdURXH0();
		return TRUE;
	}
	else if(Uartnum==1){
		while(!(rUTRSTAT1 & 0x1));//Receive data read
		*Revdata=RdURXH1();
		return TRUE;
	}
	else{
		while(!(rUTRSTAT2 & 0x1));//Receive data read
		*Revdata=RdURXH2();
		return TRUE;
	}
}	*/

⌨️ 快捷键说明

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