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

📄 tcp.c

📁 DSP2812资料,包括原理图,测试程序,使用说明,jjzhang166@163.com
💻 C
📖 第 1 页 / 共 2 页
字号:
   				}
/***************************************/
//以下为终止DMA操作
			Reg0b=0x00; 
			Reg0a=0x00;
			Reg00=0x22;			//结束或放弃DMA操作
/***************************************/

			for(i=0;i<6;i++)	//最多重发6次
				{
					for(ii=0;ii<1000;ii++)
    					{//检查CR寄存器的txp位是否为低,为1说明正在发送,为0说明发完或出错放弃
        					if((Reg00&0x04)==0)  
        						{ break; }
        				}
					if((Reg04&0x01)!=0)//表示发送成功,判断发送状态寄存器TSR,决定是否出错
						{break;};
					Reg00=0x3e;
				}

			Reg07=0xff;
			if(TxBufFlag)
				{
  					Reg04=0x40;   //txd packet start;
  				}
			else
				{
     				Reg04=0x46;  //txd packet start;
    			}
			Reg06=length>>8;  //high byte counter
			Reg05=length&0xff;//low byte counter
			Reg07=0xff;
			Reg00=0x3e;       //to sendpacket;
	}

/**********************************************************************
**函数原型:    Uint16        Rec_NewPacket()
**入口参数:		无
**出口参数:		无
**返 回 值:	0          没有新数据包
**              1          接收到新数据包
**说    明:	查询是否有新数据包并接收进缓冲区
************************************************************************/
Uint16	RecFrame()
		{
			Uint16	i,ii;
			Uint16	Temp,length;
			union{
					Uint16	total;	
					struct{
							Uint16	high:8;
							Uint16	low:8;
						  }e;
				 }d;
				 		  		
			page(0);
			bnry = Reg03;				//bnry page have read 读页指针
			bnry = bnry & 0x00FF;
			page(1);
			curr = Reg07;				//curr writepoint 8019写页指针
			curr = curr & 0x00FF;
			page(0);
			if((curr==0))return(0);	//读的过程出错
			bnry++;
			if(bnry>0x7f)	bnry=0x4c;
			if(bnry!=curr)			//此时表示有新的数据包在缓冲区里
    			{					//在任何操作都最好返回page0
     				page(0);
     //=======================================
     				Reg09=bnry;		//读页地址的高字节
     				Reg08=0x00; 	//读页地址的低字节
     				Reg0b=0x00;		//读取字节计数高字节
     				Reg0a=18;       //读取字节计数高字节
     				Reg00=0x0a;     //启动Remote DMA读操作
	 
	 
     				for(i=0;i<9;i++)	//读取一包的前18个字节:4字节的8019头部,6字节目的地址,6字节原地址,2字节协议
                    	{
                       		RxNetBuf[i]=Reg10;
                      	}
 //=======================================中止DMA操作
     				Reg0b=0x00;	//
     				Reg0a=0x00;	//
     				Reg00=0x22;	//结束或放弃DMA操作
 //=======================================
					
					d.total = RxNetBuf[0];
					length = RxNetBuf[1];
     				if(((d.e.high & 0x01)==0)||(d.e.low > 0x7f) || (d.e.low < 0x4c)||( length > 1517))
    					{//接收状态错误或下一数据包的起始页地址错误或接收的数据包长度>1500字节
       						page(1);
							curr=Reg07; 	//page1
							page(0);		//切换回page0
        					bnry = curr -1;
        					if(bnry < 0x4c) 	bnry =0x7f;
        					Reg03=bnry; 	//write to bnry     
        					Reg07=0xff;
							return(0);
              			}
//=============================================
            
     				else//表示数据包是完好的.读取剩下的数据
     					{
     						Temp = SwapByte(RxNetBuf[8]);		
        					if((Temp == IP_FRAME)||(Temp == ARP_FRAME))
								{ //协议为IP或ARP才接收
                					Reg09 = bnry;   				//read page address high
                					Reg08 = 4;      				//read page address low
                					Reg0b = length>>8;  //read count high
                					Reg0a = length & 0xff;//read count low;
                					
                					Reg00=0x0a;                             //read dma
						        	length=(length+1)/2;
                					for(ii=2;ii<length+2;ii++)
                						{
                							RxNetBuf[ii]=Reg10;
                						}
                        			//================终止DMA操作
                        			Reg0b=0x00;			//read count high   
                        			Reg0a=0x00;			//read count low;
                        			Reg00=0x22;			//结束或放弃DMA
                        			//============================
                        		}
                        		//=========================================
                        	d.total = RxNetBuf[0];
                        	bnry = d.e.low - 1;	
                			if(bnry<0x4c)	bnry=0x7f;
	        				Reg03=bnry;      //write to bnry
                			Reg07=0xff;
                			return(1);   //have new packet
               			}
   				}
			else	
				return(0);
		}



//============================================================
void	LoadEnetHrd(Uint16	MAC_type,Uint16	PROTO_type)
		{       
       		if(MAC_type==0)              		//添加目的MAC地址
         		memcpy(TxNetBuf,&MAC_server,3);
       		else if(MAC_type==BroadCast)
         		memcpy(TxNetBuf,&MAC_broad,3);
       		else if(MAC_type==RequestArp)
         		memcpy(TxNetBuf,&MAC_broad,3);
         	else if(MAC_type==AnswerArp)
         		memcpy(TxNetBuf,&MAC_rec,3);	
       		else if(MAC_type==Nod)
         		memcpy(TxNetBuf,&MAC_nod,3);
         
       		memcpy(TxNetBuf+3,&MAC_self,3);   //添加源MAC地址

       		if(PROTO_type==UDP)          	  //协议类型
         		*(TxNetBuf+6)=8+(0<<8);   
       		else if(PROTO_type==ARP)
         		*(TxNetBuf+6)=8+(6<<8);   
       		else if(PROTO_type==LSS)          //协议类型
         		*(TxNetBuf+6)=8+(8<<8);   
		}
			
void	LoadArp(struct	ipaddr	*ip,Uint16	type)
		{
			struct 	arp  arp1;
         	arp1.hard_type  = (Uint16)1<<8;
         	arp1.proto_type = (Uint16)((2048<<8)+(2048>>8));
         	arp1.proto_hard_length = 6;
         	arp1.proto_hard_length += (Uint16)4<<8;
         
         	arp1.send_macaddr =MAC_self;
         	arp1.send_ipaddr  =nod_ipaddr;

			if (type==RequestArp)
				{
		           	arp1.op_code = 0x01<<8;
   					arp1.rec_macaddr.addr2_1  =0;
		   			arp1.rec_macaddr.addr4_3  =0;
   					arp1.rec_macaddr.addr6_5  =0;
         		}
         	else{
         			arp1.op_code = 0x02<<8;
		   			arp1.rec_macaddr =MAC_rec; 			
         		}	
         	arp1.rec_ipaddr   =*ip;         
         	memcpy(TxNetBuf+7,&arp1,sizeof(arp1));
		
		}
		
void	LoadIp(struct	ipaddr	*ip,Uint16	protocal)
		{
			struct	iphdr	tip;
    		tip.tos_version = 0x45;
    		tip.tos_version += (Uint16)0<<8;      //usally not use
    		tip.id =20;  //++IP_id;
    		tip.id +=(Uint16)52<<8;
    
    		tip.frag_off =0;
    		tip.protocal_ttl   =128;       //usally 32 or 64
    		tip.saddr =nod_ipaddr;
    		tip.daddr = *ip;
			if	(protocal==TCP)
  		  		t_iphdr.protocal_ttl +=(Uint16)6<<8;
  		  	else if (protocal==UDP)
  		  		t_iphdr.protocal_ttl +=(Uint16)17<<8;	
			else if (protocal==ICMP)
	    		t_iphdr.protocal_ttl +=(Uint16)1<<8;
			else if (protocal==IGMP)
	    		t_iphdr.protocal_ttl +=(Uint16)2<<8;
         	memcpy(TxNetBuf+7,&tip,sizeof(tip));	    		
		}
		
				
void	ArpRequest(struct	ipaddr *ip)
		{
			LoadEnetHrd(RequestArp,ARP);         	
			LoadArp(ip,RequestArp);
         	SendFrame(TxNetBuf,30);
		}

void	ArpAnswer(struct	ipaddr *ip)
		{
			LoadEnetHrd(AnswerArp,ARP);         	
			LoadArp(ip,AnswerArp);
         	SendFrame(TxNetBuf,30);
		}

Uint16	SwapByte(Uint16	value)
		{
    		Uint16	temp;
    		temp=(value<<8)|(value>>8);
    		return(temp);
		}								

		
//======================= END ===============================

⌨️ 快捷键说明

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