?? net.c
字號:
}
//***********************************************************
void Reset_8019(void)
{
unsigned char i;
unsigned char j;
Reset_High;
for(i=0;i<255;i++)
for(j=0;j<255;j++);
Reset_Low;
for(i=0;i<255;i++)
for(j=0;j<255;j++);
}
//***********************************************************
/*void read_mac_add(void)
{
unsigned char i;
page_select(0);
reg09=0x00; //讀取物理地址的ram地址為0x0000
reg08=0x00;
reg0b=0x00;
reg0a=0x12;
reg00=0x0a; //020讀RTL8019
for(i=0;i<6;i++)
{
mac_add[i]=reg10;
}
}*/
//***********************************************************
void page_select(unsigned char pagenumber)
{
unsigned char data temp;
temp=reg00;
temp=temp&0x3B ;
pagenumber=pagenumber <<6;
temp=temp | pagenumber;
reg00=temp;
}
//***********************************************************
//***********************************************************
void SYSCLK_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 18.432MHz crystal
for (i=0; i < 256; i++) ; // Wait for osc. to start up
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
}
//***********************************************************
void write_mac_add(void)
{
page_select(1);
reg01=hard_add[0];
reg02=hard_add[1];
reg03=hard_add[2];
reg04=hard_add[3];
reg05=hard_add[4];
reg06=hard_add[5];
page_select(0);
}
//***********************************************************
void RTL8019_send(void)
{
unsigned int length=64;
unsigned char i;
reg09=0x40; //設置發送起始頁高地址
reg08=0x00; //發送起始頁地地址
reg0b=length>>8; //字節計數器高字節
reg0a=length&0xff; //字節計數器低字節
reg00=0x12; //020寫RTL8019
for(i=0;i<length;i++)
{
reg10=Send_Buff[i];
}
reg0b=0x00;
reg0a=0x00;
reg00=0x22; //斷開020與RTL8019的連接
for(i=0;i<6;i++) //開始發送數據,最多重發六次
{
for(i=0;i<1000;i++)
{
if((reg00&0x04)==0) //查詢TXP位確認發送狀態
break;
}
if((reg04&0x01)!=0) //查詢PTX位確認發送是否完成切無錯誤
break;
reg00=0x3e; //發送數據包
}
reg07=0xff; //清中斷
reg04=0x40; //發送緩沖起始頁
reg06=length>>8; //發送字節高位
reg05=length&0xff; //發送字節低位
reg07=0xff; //清中斷
reg00=0x3e;
}
//***********************************************************************
//***********************************************************************
bit RTL8019_receive(void)
{
unsigned int X_length;
unsigned int i;
page_select(0);
bnry=reg03;
page_select(1);
curr=reg07;
page_select(0);
if((curr==0))
return 0; //讀的過程出錯
bnry=bnry++;
if(bnry>0x7f)
bnry=0x4c;
if(bnry!=curr)
{
page_select(0);
reg09=bnry;
reg08=0x00;
reg0b=0x00;
reg0a=18;
reg00=0x0a;
for(i=0;i<18;i++)
{
Receive_Buff[i]=reg10;
}
reg0b=0x00;
reg0a=0x00;
reg00=0x22;
Protocol=Receive_Buff[16];
Protocol=Protocol<<8;
Protocol+=Receive_Buff[17];
X_length=Receive_Buff[3];
X_length<<=8;
X_length+=Receive_Buff[2];
if((Receive_Buff[1]>0x7f)||(Receive_Buff[1]<0x4c)||(Receive_Buff[3])>0x06)
{
page_select(1);
curr=reg07;
page_select(0);
bnry=curr-1;
if(bnry<0x4c)
bnry=0x7f;
reg03=bnry;
reg07=0xff;
return 0;
}
else
{
reg09=bnry;
reg08=0x00;
reg0b=X_length>>8;
reg0a=X_length&0xff;
reg00=0x0a;
for(i=0;i<X_length;i++)
{
Receive_Buff[i]=reg10;
}
reg0b=0x00;
reg0a=0x00;
reg00=0x22;
bnry=Receive_Buff[1]-1;
if(bnry>0x7f)
bnry=0x4c;
reg03=bnry;
reg07=0xff;
return 1;
}
}
}
//***********OKOKOKOKOKOKOK*************************
void ARP_Request()
{
unsigned char i;
for(i=0;i<6;i++)
{
Send_Buff[i]=0xff;
Send_Buff[i+6]=hard_add[i];
}
Send_Buff[12]=0x08;
Send_Buff[13]=0x06;
Send_Buff[14]=0x00;
Send_Buff[15]=0x01;
Send_Buff[16]=0x08;
Send_Buff[17]=0x00;
Send_Buff[18]=0x06;
Send_Buff[19]=0x04;
Send_Buff[20]=0x00;
Send_Buff[21]=0x01;
Send_Buff[28]=0x0a;
Send_Buff[29]=0x05;
Send_Buff[30]=0x84;
Send_Buff[31]=0xd7;
Send_Buff[38]=0x0a;
Send_Buff[39]=0x05;
Send_Buff[40]=0x84;
Send_Buff[41]=0x37;
for(i=0;i<6;i++)
{
Send_Buff[i+22]=hard_add[i];
Send_Buff[i+32]=0x00;
}
for(i=42;i<64;i++)
{
Send_Buff[i]=i;
}
RTL8019_send();
}
//************************************************************
void RARP(void)
{
unsigned int i;
for(i=0;i<558;i++)
Send_Buff[i]=0x00;
for(i=0;i<6;i++)
{
Send_Buff[i]=Receive_Buff[i+10];
Send_Buff[i+6]=hard_add[i];
}
Send_Buff[12]=0x08;
Send_Buff[13]=0x06;
Send_Buff[14]=0x00;
Send_Buff[15]=0x01;
Send_Buff[16]=0x08;
Send_Buff[17]=0x00;
Send_Buff[18]=0x06;
Send_Buff[19]=0x04;
Send_Buff[20]=0x00;
Send_Buff[21]=0x02;
for(i=22;i<28;i++)
Send_Buff[i]=hard_add[i-22];
for(i=28;i<32;i++)
Send_Buff[i]=IP_add[i-28];
for(i=32;i<38;i++)
Send_Buff[i]=Receive_Buff[i-22];
for(i=38;i<42;i++)
Send_Buff[i]=Receive_Buff[i-6];
RTL8019_send();
}
//****************************************************************
void ACK_Send(unsigned int count)
{
unsigned int i;
for(i=0;i<558;i++)
Send_Buff[i]=0x00;
Send_Buff[42]=0x00;
Send_Buff[43]=0x02; //填入包類型
Send_Buff[44]=count>>8;
Send_Buff[45]=count; //填入當前包數
for(i=0;i<42;i++)
Send_Buff[i]=pac_head[i]; //填入IP頭
for(i=0;i<6;i++)
pac_head[i]=A_m_a[i]; //填入目的物理地址
// pac_head[0]=0x00;
// pac_head[1]=0x13;
// pac_head[2]=0xd4;
// pac_head[3]=0x3e;
// pac_head[4]=0x58;
// pac_head[5]=0x67;
for(i=0;i<4;i++)
pac_head[30+i]=A_IP_add[i]; //填入目的IP地址
//pac_head[30]=0x0a;
//pac_head[31]=0x05;
//pac_head[32]=0x84;
//pac_head[33]=0x37;
H_H_Length=0x00; //IP報總長度高位
L_H_Length=0x2e; //IP包總長度低位
H_Loc_Port=Loc_Port>>8;
L_Loc_Port=Loc_Port; //填入源端口
H_Aim_Port=Aim_Port>>8;
L_Aim_Port=Aim_Port; //填入目的端口
H_UDP_Length=0x00;
L_UDP_Length=0x1E; //填入UDP長度
for(i=0;i<42;i++)
Send_Buff[i]=pac_head[i]; //填入IP頭
kk=CRC_Process(20,14,0);
H_CRC=kk>>8;
L_CRC=kk; //填入IP首部校驗和
kk=CRC_Process(30,34,1); //求UDP校驗和
HUDPCRC=kk>>8;
LUDPCRC=kk; //填入校驗和
RTL8019_send(); //發送
}
//****************************************************************
unsigned int CRC_Process(unsigned char LEN,unsigned char mov,unsigned char type)
{
unsigned int b;
unsigned long Crc=0x0000;
unsigned int i;
//p=a;
for(i=0;i<LEN;i+=2)
{
//a[i]=~a[i];
//printf("%x ",a[i]);
b=Send_Buff[i+mov];
b<<=8;
b+=Send_Buff[i+mov+1];
Crc+=b;
}
if(type)
{
for(i=0;i<13;i++)
UDP_F_Head[i]=0x00;
for(i=0;i<4;i++)
{
UDP_F_Head[i]=IP_add[i];
UDP_F_Head[i+4]=A_IP_add[i];
}
UDP_F_Head[9]=0x11;
UDP_F_Head[10]=0x00;
UDP_F_Head[11]=0x1e;
for(i=0;i<12;i+=2)
{
b=UDP_F_Head[i];
b<<=8;
b+=UDP_F_Head[i+1];
Crc+=b;
}
}
//Crc=~Crc;
Crc=((Crc>>16)&0xffff)+(Crc&0xffff);
if(Crc&0xffff0000)
Crc++;
return ((unsigned int)(~(Crc&0xffff)));
}
//*******************************************************
void Pag_TimeOut(void)
{
ACK_Send(Package); //重發ACK包
}
//*******************************************************
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -