51单片机RTL8019AS网卡驱动程序
}
if(txd_buffer_select) reg04=0x40; //txd packet start;
else reg04=0x46; //txd packet start;
reg06=length>>8; //high byte counter
reg05=length0xFF; //low byte counter
reg00=0x3E; //to sendpacket;
}
bit recv_packet(union netcard *rxdnet)//ne2000收包子程序
{
unsigned char i;
unsigned int ii;
unsigned char bnry,curr;
page(0);
reg07=0xFF;
bnry=reg03; //bnry page have read 读页指针
page(1);
curr=reg07; //curr writepoint 8019写页指针
page(0);
if(curr==0)
return 0; //读的过程出错
bnry=bnry++;
if(bnry>0x7F) bnry=0x4C;
if(bnry!=curr){ //此时表示有新的数据包在缓冲区里
//读取一包的前18个字节:4字节的8019头部,6字节目的地址,6字节原地址,2字节协议
//在任何操作都最好返回page0
page(0);
reg09=bnry; //read page address high
reg08=0x00; //read page address low
reg0b=0x00; //read count high
reg0a=18; //read count low;
reg00=0x0A; //read dma
for(i=0;i18;i++)
rxdnet->bytes.bytebuf[i]=reg10;
i=rxdnet->bytes.bytebuf[3]; //将长度字段的高低字节掉转
rxdnet->bytes.bytebuf[3]=rxdnet->bytes.bytebuf[2];
rxdnet->bytes.bytebuf[2]=i;
rxdnet->etherframe.length=rxdnet->etherframe.length-4; //去掉4个字节的CRC
//表示读入的数据包有效
if(((rxdnet->bytes.bytebuf[0]0x01)==0)||(rxdnet->bytes.bytebuf[1]>0x7F)||(rxdnet->bytes.bytebuf[1]0x4C)||(rxdnet->bytes.bytebuf[2]>0x06)){
//接收状态错误,或者next_page_start错误或者长度错误,将丢弃所有数据包
page(1);
curr=reg07; //page1
page(0); //切换回page0
bnry=curr-1;
if(bnry0x4C) bnry=0x7F;
reg03=bnry; //write to bnry
return 0;
}
else{//表示数据包是完好的.读取剩下的数据
if((rxdnet->etherframe.protocal==0x0800)||(rxdnet->etherframe.protocal==0x0806)){
//协议为IP或ARP才接收
reg09=bnry; //read page address high
reg08=4; //read page address low
reg0b=rxdnet->etherframe.length>>8; //read count high
reg0a=rxdnet->etherframe.length0xFF; //read count low;
reg00=0x0A; //read dma
for(ii=4;iietherframe.length+4;ii++)
rxdnet->bytes.bytebuf[ii]=reg10;
}
bnry=rxdnet->bytes.bytebuf[1]-1;//next page start-1
if(bnry0x4C) bnry=0x7F;
reg03=bnry; //write to bnry
return 1; //have new packet
}
}
return 0;
}
51单片机相关文章:51单片机教程
评论