www.pudn.com > 44B0X_uCOS276.rar > 44blib.c
#include "..\inc\44b.h" #include "..\inc\44blib.h" #include "..\inc\def.h" #include "..\inc\option.h" #include#include #include #include #include #define STACKSIZE 0xa00 //SVC satck size(do not use user stack) #define HEAPEND (_ISR_STARTADDRESS-STACKSIZE-0x500) // = 0xc7ff000 //SVC Stack Area:0xc(e)7ff000-0xc(e)7ffaff extern char Image$$RW$$Limit[]; void *mallocPt=Image$$RW$$Limit; //*************************************************************************** void Led_Set(char data) { *((unsigned char *)0x2000000)=~(data << 4); } void Seg7_Set(char data) { unsigned char aTemp; switch(data) { case 0x0: aTemp = 0xc0; break; case 0x1: aTemp = 0xf9; break; case 0x2: aTemp = 0xa4; break; case 0x3: aTemp = 0xb0; break; case 0x4: aTemp = 0x99; break; case 0x5: aTemp = 0x92; break; case 0x6: aTemp = 0x82; break; case 0x7: aTemp = 0xf8; break; case 0x8: aTemp = 0x80; break; case 0x9: aTemp = 0x90; break; case 0xa: aTemp = 0x88; break; case 0xb: aTemp = 0x83; break; case 0xc: aTemp = 0xc6; break; case 0xd: aTemp = 0xa1; break; case 0xe: aTemp = 0x86; break; case 0xf: aTemp = 0x8e; break; default: break; } *((unsigned char *)0x8000000) = aTemp; } /**************************************************************************** 【功能说明】通用延时函数,延时time个100us time=0: adjust the Delay function by WatchDog timer. time>0: the number of loop time 100us resolution. ****************************************************************************/ void Delay(int time) { int i; for(;time>0;time--) for(i=0;i<400;i++); } //*************************************************************************** /**************************************************************************** 【功能说明】选择串口通道为COM0或者COM1 ****************************************************************************/ static int whichUart=0; void Uart_Select(int ch) { whichUart=ch; } //*************************************************************************** /**************************************************************************** 【功能说明】异步串行口初始化 ****************************************************************************/ 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_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; } } //*************************************************************************** /**************************************************************************** 【功能说明】从串口接收一个字符,如果没有收到数据返回0 ****************************************************************************/ 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. #define MAX_TBUF 1000 static char tbuf[MAX_TBUF]= {0, }; 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); */ va_list v_list; char *ptr; int i= 0; va_start(v_list, fmt); // Initialize variable arguments. vsprintf(tbuf, fmt, v_list ); va_end(v_list); ptr= tbuf; while( (*ptr) && i 0); //to align 4byte if( (int)mallocPt > HEAPEND ) { mallocPt=returnPt; return NULL; } return returnPt; } //*************************************************************************** /**************************************************************************** 【功能说明】释放内存空间 ****************************************************************************/ void free(void *pt) { mallocPt=pt; } //*************************************************************************** /**************************************************************************** 【功能说明】 ****************************************************************************/ void Cache_Flush(void) { int i,saveSyscfg; saveSyscfg=rSYSCFG; rSYSCFG=SYSCFG_0KB; for(i=0x10004000;i<0x10004800;i+=16) { *((int *)i)=0x0; } rSYSCFG=saveSyscfg; } //***************************************************************************