高级会员
- 积分
- 555
- 金钱
- 555
- 注册时间
- 2015-6-11
- 在线时间
- 66 小时
|
发表于 2018-3-10 11:57:09
|
显示全部楼层
本帖最后由 heey 于 2018-3-10 12:00 编辑
ENC28J60勘误表,好像芯片有问题,通过程序避免。我回去找找例程
unsigned int enc28j60PacketReceive(unsigned int maxlen, unsigned char* packet)
{
unsigned int rxstat;//接收状态
unsigned int len; //接收长度
//如果检查到一个包被接收并且缓存
if(!(enc28j60Read(EIR) & EIR_PKTIF))
{
//勘误表#6,PKTIF是不可靠的,双重检查【PKTIF】和【EPKTCNT】
if(enc28j60Read(EPKTCNT) == 0)
{
return(0);
}
}
//设置读指针开始收到包
enc28j60Write(ERDPTL, (NextPacketPtr));
enc28j60Write(ERDPTH, (NextPacketPtr)>>8);
//读取下一个数据包的指针
NextPacketPtr = enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0);
NextPacketPtr |= enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0)<<8;
//读取下一个数据包的长度
len = enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0); //先读回低字节
len |= enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0)<<8; //后读回高字节
len -= 4;//-CRC
//读取接收状态
rxstat = enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0);
rxstat |= enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0)<<8;
if(len<=maxlen)//如果包长小于等于缓冲区大小,则接收包
{
enc28j60ReadBuffer(len, packet);//读取数据包
{//前移接收读指针:释放缓冲区1/2
unsigned int rs,re;
rs = enc28j60Read(ERXSTH);
rs <<= 8;
rs |= enc28j60Read(ERXSTL);
re = enc28j60Read(ERXNDH);
re <<= 8;
re |= enc28j60Read(ERXNDL);
if (NextPacketPtr - 1 < rs || NextPacketPtr - 1 > re)
{
enc28j60Write(ERXRDPTL, (re));
enc28j60Write(ERXRDPTH, (re)>>8);
}
else
{
enc28j60Write(ERXRDPTL, (NextPacketPtr-1));
enc28j60Write(ERXRDPTH, (NextPacketPtr-1)>>8);
}
}
//包计数器减一:释放缓冲区2/2
enc28j60WriteOp(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC);
return(len);//返回包长度
}
else//包长大于缓冲区长度,丢弃,但仍然要释放缓冲区
{
{
unsigned int rs,re;
rs = enc28j60Read(ERXSTH);
rs <<= 8;
rs |= enc28j60Read(ERXSTL);
re = enc28j60Read(ERXNDH);
re <<= 8;
re |= enc28j60Read(ERXNDL);
if (NextPacketPtr - 1 < rs || NextPacketPtr - 1 > re)
{
enc28j60Write(ERXRDPTL, (re));
enc28j60Write(ERXRDPTH, (re)>>8);
}
else
{
enc28j60Write(ERXRDPTL, (NextPacketPtr-1));
enc28j60Write(ERXRDPTH, (NextPacketPtr-1)>>8);
}
}
enc28j60WriteOp(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC);
return(0);//我们丢弃了数据包,所以返回0
}
}
|
|