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

📄 func.c

📁 完整的TCP/IP源代码,绝对好用
💻 C
字号:
#include "cc.h"
#include "ip51_sfr.h"
#include "func.h"
#include "target.h"
#include "ip51_mac.h"
#include <string.h>

extern UINT8 xdata MY_IP[4];
extern UINT8 xdata FTP_SERVER_ADDR[4];


 UINT16	timer_val = 0;
unsigned int Check_sum(unsigned char* data_ptr,unsigned int len)
{
	unsigned int i;
	long2int temp ;
	temp.i = 0;
    for(i=0;i<len/2;i++)
    {
	temp.i += *(unsigned int*)data_ptr;
	temp.integer.int_data2 += temp.integer.int_data1;
    temp.integer.int_data1 = 0;
    data_ptr += 2;
    }
    if(len & 0x01)
    {
//	temp.i += *(unsigned int*)data_ptr;
    temp.i += (*(unsigned int*)data_ptr & 0xff00);
    }
	temp.integer.int_data2 += temp.integer.int_data1;
 
	return temp.integer.int_data2 ;
    //return 0;
}


/*
UINT16 Check_sum(UINT8* data_ptr,UINT16 len)
{
	UINT8	couten_bk;
	UINT16	res;
	//EA = 0;
	EIE &= 0XF7;
	couten_bk	=	COUTEN;
	//select BOOT DMA
	COUTEN	|=	0x30;
	//initial start value
//    BTTPAG  =   GET_PAGE_NUM(data_ptr);
    BTSPAG  =   BTTPAG;
    //BTSPAG  =1;
	//BTTPAG  =1;
	SUMOL	=	0xff;
	SUMOH	=	0xff;
	BTSCPL	=	ADDRLOW8(data_ptr);
	BTSCPH	=	ADDRHIGH8(data_ptr);
	BTSEDL	=	0xff;
	BTSEDH	=	0xff;
	BTTCPL	=	ADDRLOW8(data_ptr);
	BTTCPH	=	ADDRHIGH8(data_ptr);
	BTTEDL	=	0xff;
	BTTEDH	=	0xff;
	BTLTHL	=	(UINT8)len;
	BTLTHH	=	(UINT8)(len >> 8);
	//set ACU to caculate CHKSUM
	CHKCTL	=	0x01;

	//start BOOT DMA
	BTCTRL	|=	0xc0;

	res	=	(SUMOL << 8)|SUMOH;

	//restore COUTEN
	COUTEN	=	couten_bk;
	//EA = 1;
	EIE |= 0X08;
	return	res;
}
*/
/*
// memcpy the 256k flash data to ram .
void FLASH_T_RAM()
{
	unsigned char couten_bk,i;
//	doffset = 0x0;
	
	couten_bk	=	COUTEN;
	//select BOOT DMA
	COUTEN	|=	0x30;
	for(i=0;i<7;i++)
	{
	   BTSHDL  =   0;	  //START ADDR
       BTSHDH  =   0X80;
	   BTSCPL	=	0;	//current
	   BTSCPH	=	0x80;
	   BTSEDL	=	0xff;
	   BTSEDH	=	0xff; //end addr
	   BTSPAG  =   0X11+i;

	   BTTHDL  =   0;
	   BTTHDH  =   0X80;
	   BTTCPL	=	00;
	   BTTCPH	=	0x80;
	   BTTEDL	=	0xff;
	   BTTEDH	=	0xff;
	   BTTPAG  =   0X01+i;

	   doffset = 0x0;
       BTLTHL	=	0;
	   BTLTHH	=	0X80;
	   BTCTRL	|=	0x80;//start memcp HARDWARE AUTO CLEAR 
	   doffset = 0x08;
	}
	//restore COUTEN
	COUTEN	=	couten_bk;
}
*/
// use 	lx51 and ax51 
//
// The pointer's length is 3 bytes. 0x00803000 ->CODE comman area 0x3000 0x00813000->code bank1 0x3000
// 0x00003000-> data common area 0x3000 
// 0x00013000-> data bank1 area 0x3000

void memcp(unsigned char  *dest,unsigned char *src,unsigned int len)
{
   memcpy(dest,src,len);
}
/*
void memcp(unsigned char  *dest,unsigned char *src,unsigned int len)
{
   long_type i1;
   unsigned char q,couten_bk;
   //EA = 0;
	EIE &= 0XF7;
  	couten_bk	=	COUTEN;
	//select BOOT DMA
	COUTEN	|=	0x30;

   i1.i = src;
   q = i1.character.cha2;// & 0x80;
   switch(q)
   {
      case 0xff:   //code common bank
	       BTSPAG = 0X10;
		   i1.i += 0x8000;
    	   BTSCPH = i1.character.cha3;
	       BTSCPL = i1.character.cha4;

           break;
      case 0:		//data common bank
	       BTSPAG = 0X0;
    	   BTSCPH = i1.character.cha3;
	       BTSCPL = i1.character.cha4;
           break;

      default:		//code 
           q = i1.character.cha2 & 0x80;
           if(q!=0)
           {
           BTSPAG = i1.character.cha2 & 0x7f;
//	       BTSPAG--;
           BTSPAG |= 0x10; 	//source page register point to flash (0x1x)
  	       BTSCPH = i1.character.cha3;
	       BTSCPL = i1.character.cha4;
           }
           else  
           {
            BTSPAG = i1.character.cha2;
//	        BTSPAG--;
   	        BTSCPH = i1.character.cha3;
	        BTSCPL = i1.character.cha4;
           }
        break;
     }
   i1.i = dest;
   q = i1.character.cha2;// & 0x80;
   switch(q)
   {
      case 0xff:
	      // debug(" err to flash write\n");
	       BTTPAG = 0X10;
		   i1.i += 0x8000;
    	   BTTCPH = i1.character.cha3;
	       BTTCPL = i1.character.cha4;

           break;
      case 0:
	       //debug("err to write idata\n");
	       BTTPAG = 0X0;
    	   BTTCPH = i1.character.cha3;
	       BTTCPL = i1.character.cha4;
           break;

      default:		//code 
           q = i1.character.cha2 & 0x80;
           if(q!=0)
           {
           BTTPAG = i1.character.cha2 & 0x7f;
//	       BTTPAG--;
           BTTPAG |= 0x10; 	//source page register point to flash (0x1x)
  	       BTTCPH = i1.character.cha3;
	       BTTCPL = i1.character.cha4;
           }
           else  
           {
            BTTPAG = i1.character.cha2;
//	        BTTPAG--;
   	        BTTCPH = i1.character.cha3;
	        BTTCPL = i1.character.cha4;
           }
        break;
     }





    BTSEDL	=	0xff;
    BTSEDH	=	0xff; //source end addr

    BTTEDL	=	0xff;
    BTTEDH	=	0xff; //dest end addr

    BTLTHL	=	(unsigned char)len;
	BTLTHH	=	(unsigned char)(len >> 8);

    BTCTRL	|=	0x80;//start memcp HARDWARE AUTO CLEAR 
	//restore COUTEN
	COUTEN	=	couten_bk;
    //EA = 1;
	EIE |= 0X08;

}
*/
/*
UINT8 GET_PAGE_NUM(UINT8 *BUF)
{
   long_type TEMP;
   UINT8  q,PAGE_NUM;
   TEMP.i = BUF;
   q = TEMP.character.cha2 & 0x80;

   if(q==0x80)		//code 
   {
      PAGE_NUM = TEMP.character.cha2 & 0x7f;
//	  PAGE_NUM--;
      PAGE_NUM |= 0x10; 	//source page register point to flash (0x1x)
   }
   else  
   {
      PAGE_NUM = TEMP.character.cha2;
//	  PAGE_NUM--;
   }


   return PAGE_NUM;
}
*/

//------------------------------------------------------------------------
// This function is the standard string search. The Keil library
// does not provide it.  It looks for one string in another string
// and returns a pointer to it if found, otherwise returns NULL. 
//------------------------------------------------------------------------
UINT8 *ch_strstr(UINT8 *haystack, UINT8 *needle)
{
	UINT8 *ptr1, *ptr2;
	
	// Protect against NULL pointer
	if (*needle == 0) return(haystack);
	for( ; *haystack; haystack++ )
	{
		// Look for needle in haystack.  If there is a
      // match then this will continue all the way
      // until ptr1 reaches the NULL at the end of needle 
		for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
							
		// If there is a match then return pointer to needle in haystack
		if(*ptr1 == 0) return(haystack);	
	}
	return NULL;			// no matching string found
}
//************************************************************
// 20MS
void  init_timer0(void)
{
	TMOD	&=	0xf0;
	TMOD	|=	0x01;//16-bit timer,12 clk per increment
	CKCON	&=	0xf7;
	TH0	= 0X56;
	TL0 = 0X74;

	ET0		=	1;//enable timer0 interrupt
	
//	TR0		=	0;//stop timer
//	TR0		=	1;//run timer
}

void  run_timer0(void)
{
//	timer_val	=	0;
	TR0		=	1;
}
void Isr_timer0(void) interrupt 1
{
	TH0	= 0X56;
	TL0 = 0X74;

	timer_val ++;

/*	if(timer_val > 100)
	{
		timer_val	=	0;
		if(uart_tbuflen() == 0)
			TR0	=	0;
			tcp_win	=	UTBUF_SIZE	-	uart_tbuflen();
			tcp_send(FLG_ACK, 20,Eth2COM_PORT, 0);
	}*/
	
	return;

}
// ******************************************************
//
// write flash mx29lv040
// 只能用于固定的BANK内容的写
// 
// ******************************************************
/*
INT8  flash_wr(UINT16 dest, UINT8 DEST_PAGE,UINT8* src, UINT16 len)
{
	UINT16	idata	i;
	UINT8	idata	d;
	UINT8	idata	bak;
	UINT8 D_PAGE0_BAK;
	D_PAGE0_BAK = D_PAGE0;
	IE	&=	0x7F;
	bak	=	CKCON;

	dest	+=	0x8000;
	CKCON	=	(CKCON & 0xf8) | 0x05;	//stretch = 5

//	Erase Sector
	D_PAGE0	=	0x10;
	*((UINT8 xdata *)(0x8555)) = 0xAA;
	*((UINT8 xdata*)(0x82AA)) = 0x55;
	*((UINT8 xdata*)(0x8555)) = 0x80;
	*((UINT8 xdata*)(0x8555)) = 0xAA;
	*((UINT8 xdata*)(0x82AA)) = 0x55;
	D_PAGE0	=	DEST_PAGE;   //The earsed sector 
	*((UINT8 xdata*)(0x8000)) = 0x30;

//	while(*((UINT8 xdata *)(0x8000)) & 0x80 == 0x00)
//	if(*((UINT8 xdata *)(0x8000)) & 0x20)
//		return -1;
//	while(*((UINT8 xdata *)(0x8000)) & 0x20 == 0x00);//wait time out

	for(i=0;i<65530;i++);	//WAIT
//	Write
	for(i=0;i<len;i++)
	{
	//D_PAGE0	=	0x01;	//ONLY FOR BANK1 DATA AREA?
	d	=	*src;//read data
	D_PAGE0	=	0x10;
	*((UINT8 xdata *)(0x8555)) = 0xAA;
	*((UINT8 xdata *)(0x82AA)) = 0x55;
	*((UINT8 xdata *)(0x8555)) = 0xA0;
	D_PAGE0	=	DEST_PAGE;
	*(UINT8 *)dest	= d;
	while(  *(UINT8 *)dest & 0x80 == 0)
	if(*((UINT8 xdata *)(0x8000)) & 0x20)
		return -1;
	src ++;dest++;
	}

	CKCON	=	bak;
	D_PAGE0 = D_PAGE0_BAK;
	IE	|=	0x80;
}
*/
//********************************************************
// read from flash and save in the sram
// 这段程序的目的是要完成将一些需要永久保存的数据从FLASH中读到程序中为其开辟的数组中.
// 此处修改了D——PAGE0
// 源地址必须是绝对寻址.即人为指定.
// 目的地址是一个指针型的变量,不需要人为指定.
/*
void  flash_rd(UINT8 *dest,UINT16 src,UINT8 src_page, UINT16 len)
{
	UINT16	idata	i;
	UINT8	idata	d;
	UINT8	idata	bak;
	UINT8 D_PAGE0_BAK;
	IE	&=	0x7F;
    D_PAGE0_BAK= D_PAGE0;

	bak	=	CKCON;
	src	+=	0x8000;
	CKCON	=	(CKCON & 0xf8) | 0x05;	//stretch = 5
	for(i=0;i<len;i++)		
	{
		D_PAGE0	=	src_page;//select Flash page 
		d	=	*(UINT8 *)src;//read data
		//D_PAGE0	=	dest_page;//select data sram page 
		*dest	=	d;//write into ram这里目的地址指针已经含有了页信息.
		src ++;dest ++;
	}
	CKCON	=	bak;
	D_PAGE0 = D_PAGE0_BAK;
	IE	|=	0x80;
}
*/
//***************************************************
//  第15个BANK的2000H开始的地方存放配置信息
//  2000H-2003H LOCAL IP ADDR
//  200AH       BAUD RATE
//  200BH       NC
//  200CH-200FH FTP SERVER IP
//  2010H-2017H FTP PASSWORD
//  2018H-2020H FTP USER NAME
/*
void GET_para()
{
  UINT8 flash_buf[100];

  flash_rd(flash_buf,0x2000,0x1f,100); //0x1f is flash page 15
  if(flash_buf[0]& flash_buf[1]&flash_buf[2]&flash_buf[3] == 0xff)
  {
     MY_IP[0] = 172;
	 MY_IP[1] = 28;
	 MY_IP[2] = 24;
	 MY_IP[3] = 52;
  }
  else
  {
     MY_IP[0] = flash_buf[0];
	 MY_IP[1] = flash_buf[1];
	 MY_IP[2] = flash_buf[2];
	 MY_IP[3] = flash_buf[3];
  }
  if(flash_buf[0X0C]& flash_buf[0X0D]&flash_buf[0X0E]&flash_buf[0X0F] == 0xff)
  {
     FTP_SERVER_ADDR[0] = 172;
	 FTP_SERVER_ADDR[1] = 28;
	 FTP_SERVER_ADDR[2] = 24;
	 FTP_SERVER_ADDR[3] = 25;
  }
  else
  {
     FTP_SERVER_ADDR[0] = flash_buf[0X0C];
	 FTP_SERVER_ADDR[1] = flash_buf[0X0d];
	 FTP_SERVER_ADDR[2] = flash_buf[0X0e];
	 FTP_SERVER_ADDR[3] = flash_buf[0X0f];

  }

   
}

*/

⌨️ 快捷键说明

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