Line 20... |
Line 20... |
#include "tcpip.h"
|
#include "tcpip.h"
|
|
|
#define POLYNOMIAL 0x04C11DB7 //CRC bit 33 is truncated
|
#define POLYNOMIAL 0x04C11DB7 //CRC bit 33 is truncated
|
#define TOPBIT (1<<31)
|
#define TOPBIT (1<<31)
|
#define BYTE_EMPTY 0xde //Data copied into receive buffer
|
#define BYTE_EMPTY 0xde //Data copied into receive buffer
|
#define COUNT_EMPTY 32 //Count to decide there isn't data
|
#define COUNT_EMPTY 16 //Count to decide there isn't data
|
#define INDEX_MASK 0xffff //Size of receive buffer
|
#define INDEX_MASK 0xffff //Size of receive buffer
|
|
|
//void dump(const unsigned char *data, int length);
|
//void dump(const unsigned char *data, int length);
|
|
|
static unsigned char gDestMac[]={0x5d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
static unsigned char gDestMac[]={0x5d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
Line 32... |
Line 32... |
static unsigned char reflect[256];
|
static unsigned char reflect[256];
|
static unsigned char reflectNibble[256];
|
static unsigned char reflectNibble[256];
|
static OS_Semaphore_t *SemEthernet, *SemEthTransmit;
|
static OS_Semaphore_t *SemEthernet, *SemEthTransmit;
|
static int gIndex; //byte index into 0x13ff0000 receive buffer
|
static int gIndex; //byte index into 0x13ff0000 receive buffer
|
static int gCheckedBefore;
|
static int gCheckedBefore;
|
|
static int gEmptyBefore;
|
|
|
|
|
//Read received data from 0x13ff0000. Data starts with 0x5d+MACaddress.
|
//Read received data from 0x13ff0000. Data starts with 0x5d+MACaddress.
|
//Data is being received while processing the data. Therefore,
|
//Data is being received while processing the data. Therefore,
|
//all errors require waiting and then re-processing the data
|
//all errors require waiting and then re-processing the data
|
Line 46... |
Line 47... |
int start, i, j, shift, offset;
|
int start, i, j, shift, offset;
|
int byte, byteNext;
|
int byte, byteNext;
|
unsigned long crc;
|
unsigned long crc;
|
int byteCrc;
|
int byteCrc;
|
volatile unsigned char *buf = (unsigned char*)ETHERNET_RECEIVE;
|
volatile unsigned char *buf = (unsigned char*)ETHERNET_RECEIVE;
|
int countEmpty, countOk, needWait;
|
int countEmpty, countEmptyGoal, countOk, needWait;
|
|
int packetExpected;
|
|
|
//Find the start of a frame
|
//Find the start of a frame
|
countEmpty = 0;
|
countEmpty = 0;
|
countOk = 0;
|
countOk = 0;
|
needWait = 0;
|
needWait = 0;
|
|
countEmptyGoal = COUNT_EMPTY;
|
|
packetExpected = MemoryRead(IRQ_STATUS) & IRQ_ETHERNET_RECEIVE;
|
|
if(packetExpected && buf[gIndex] == BYTE_EMPTY && gEmptyBefore)
|
|
{
|
|
//printf("Check ");
|
|
countEmptyGoal = 1500;
|
|
}
|
|
MemoryRead(ETHERNET_REG); //clear receive interrupt
|
for(i = 0; i < INDEX_MASK; ++i)
|
for(i = 0; i < INDEX_MASK; ++i)
|
{
|
{
|
//Check if partial packet possibly received
|
//Check if partial packet possibly received
|
if(needWait && gCheckedBefore == 0 && countOk != i && countEmpty != i)
|
if(needWait && gCheckedBefore == 0 && countOk != i && countEmpty != i)
|
{
|
{
|
Line 95... |
Line 105... |
}
|
}
|
|
|
//Check if remainder of buffer is empty
|
//Check if remainder of buffer is empty
|
if(byte == BYTE_EMPTY)
|
if(byte == BYTE_EMPTY)
|
{
|
{
|
if(++countEmpty >= COUNT_EMPTY)
|
if(++countEmpty >= countEmptyGoal)
|
{
|
{
|
//Set skiped bytes to BYTE_EMPTY
|
//Set skiped bytes to BYTE_EMPTY
|
//if(i - COUNT_EMPTY > 3)
|
//if(i - countEmpty > 3)
|
//{
|
//{
|
// printf("eb%d \n", i - COUNT_EMPTY);
|
// printf("eb%d \n", i - countEmpty);
|
// //dump((char*)buf+gIndex, 0x200);
|
// //dump((char*)buf+gIndex, 0x200);
|
//}
|
//}
|
for(j = 0; j <= i - COUNT_EMPTY; ++j)
|
for(j = 0; j <= i - countEmpty; ++j)
|
{
|
{
|
buf[gIndex] = BYTE_EMPTY;
|
buf[gIndex] = BYTE_EMPTY;
|
gIndex = (gIndex + 1) & INDEX_MASK;
|
gIndex = (gIndex + 1) & INDEX_MASK;
|
}
|
}
|
gCheckedBefore = 0;
|
gCheckedBefore = 0;
|
|
if(countEmpty >= i && packetExpected)
|
|
gEmptyBefore = 1;
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
else
|
else
|
{
|
{
|
if(countEmpty > 2 || (countEmpty > 0 && countEmpty == i))
|
if(countEmpty > 2 || (countEmpty > 0 && countEmpty == i))
|
needWait = 1;
|
needWait = 1;
|
countEmpty = 0;
|
countEmpty = 0;
|
|
gEmptyBefore = 0;
|
}
|
}
|
}
|
}
|
|
|
//Found start of frame. Now find end of frame and check CRC.
|
//Found start of frame. Now find end of frame and check CRC.
|
start = gIndex;
|
start = gIndex;
|
Line 133... |
Line 146... |
|
|
byte = ((byte << 4) & 0xf0) | (byte >> 4); //swap nibbles
|
byte = ((byte << 4) & 0xf0) | (byte >> 4); //swap nibbles
|
buffer[count++] = (unsigned char)byte;
|
buffer[count++] = (unsigned char)byte;
|
byte = reflect[byte] ^ (crc >> 24); //calculate CRC32
|
byte = reflect[byte] ^ (crc >> 24); //calculate CRC32
|
crc = CrcTable[byte] ^ (crc << 8);
|
crc = CrcTable[byte] ^ (crc << 8);
|
if(count >= 60)
|
if(count >= 40)
|
{
|
{
|
//Check if CRC matches to detect end of frame
|
//Check if CRC matches to detect end of frame
|
byteCrc = reflectNibble[crc >> 24];
|
byteCrc = reflectNibble[crc >> 24];
|
byteNext = buf[gIndex];
|
byteNext = buf[gIndex];
|
if(byteCrc == byteNext)
|
if(byteCrc == byteNext)
|
Line 254... |
Line 267... |
{
|
{
|
if(ethFrame == NULL)
|
if(ethFrame == NULL)
|
ethFrame = IPFrameGet(FRAME_COUNT_RCV);
|
ethFrame = IPFrameGet(FRAME_COUNT_RCV);
|
if(ethFrame == NULL)
|
if(ethFrame == NULL)
|
break;
|
break;
|
MemoryRead(ETHERNET_REG); //clear receive interrupt
|
|
length = EthernetReceive(ethFrame->packet, PACKET_SIZE);
|
length = EthernetReceive(ethFrame->packet, PACKET_SIZE);
|
if(length == 0)
|
if(length == 0)
|
break;
|
break;
|
Led(1);
|
Led(1);
|
rc = IPProcessEthernetPacket(ethFrame, length);
|
rc = IPProcessEthernetPacket(ethFrame, length);
|