Line 31... |
Line 31... |
static unsigned int CrcTable[256];
|
static unsigned int CrcTable[256];
|
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 gCrcChecked;
|
|
|
|
|
//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
|
//to see if the error is fixed by receiving the rest of the packet.
|
//to see if the error is fixed by receiving the rest of the packet.
|
int EthernetReceive(unsigned char *buffer, int length)
|
int EthernetReceive(unsigned char *buffer, int length)
|
{
|
{
|
int count;
|
int count;
|
int start, i, j, shift, offset, index;
|
int start, i, j, shift, offset, index, emptyCount;
|
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 packetExpected;
|
int packetExpected;
|
|
|
//Find the start of a frame
|
//Find the start of a frame
|
packetExpected = MemoryRead(IRQ_STATUS) & IRQ_ETHERNET_RECEIVE;
|
packetExpected = MemoryRead(IRQ_STATUS) & IRQ_ETHERNET_RECEIVE;
|
MemoryRead(ETHERNET_REG); //clear receive interrupt
|
MemoryRead(ETHERNET_REG); //clear receive interrupt
|
|
emptyCount = 0;
|
|
|
//Find dest MAC address
|
//Find dest MAC address
|
for(offset = 0; offset <= INDEX_MASK; ++offset)
|
for(offset = 0; offset <= INDEX_MASK; ++offset)
|
{
|
{
|
index = (gIndex + offset) & INDEX_MASK;
|
index = (gIndex + offset) & INDEX_MASK;
|
Line 68... |
Line 69... |
if(byte != 0xff && byte != gDestMac[i])
|
if(byte != 0xff && byte != gDestMac[i])
|
break;
|
break;
|
}
|
}
|
if(i == sizeof(gDestMac))
|
if(i == sizeof(gDestMac))
|
break; //found dest MAC
|
break; //found dest MAC
|
|
emptyCount = 0;
|
}
|
}
|
else if(byte == BYTE_EMPTY && packetExpected == 0)
|
else if(byte == BYTE_EMPTY)
|
|
{
|
|
if(packetExpected == 0 && ++emptyCount >= 4)
|
return 0;
|
return 0;
|
}
|
}
|
|
else
|
|
emptyCount = 0;
|
|
}
|
if(offset > INDEX_MASK)
|
if(offset > INDEX_MASK)
|
return 0;
|
return 0;
|
while(gIndex != index)
|
while(gIndex != index)
|
{
|
{
|
buf[gIndex] = BYTE_EMPTY;
|
buf[gIndex] = BYTE_EMPTY;
|
Line 123... |
Line 130... |
while(gIndex & 3)
|
while(gIndex & 3)
|
{
|
{
|
buf[gIndex] = BYTE_EMPTY;
|
buf[gIndex] = BYTE_EMPTY;
|
gIndex = (gIndex + 1) & INDEX_MASK;
|
gIndex = (gIndex + 1) & INDEX_MASK;
|
}
|
}
|
gCheckedBefore = 0;
|
gCrcChecked = 0;
|
return count;
|
return count;
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
gIndex = start;
|
gIndex = start;
|
if(gCheckedBefore++ > 1)
|
if(++gCrcChecked > 0) //if the CPU speed is > 25MHz, change to 1
|
{
|
{
|
buf[gIndex] = BYTE_EMPTY;
|
buf[gIndex] = BYTE_EMPTY;
|
gIndex = (gIndex + 1) & INDEX_MASK;
|
gIndex = (gIndex + 1) & INDEX_MASK;
|
}
|
}
|
return 0; //wait for more data
|
return -1;
|
}
|
}
|
|
|
|
|
//Copy transmit data to 0x13fe0000 with preamble and CRC32
|
//Copy transmit data to 0x13fe0000 with preamble and CRC32
|
void EthernetTransmit(unsigned char *buffer, int length)
|
void EthernetTransmit(unsigned char *buffer, int length)
|
Line 198... |
Line 205... |
void EthernetThread(void *arg)
|
void EthernetThread(void *arg)
|
{
|
{
|
int length;
|
int length;
|
int rc;
|
int rc;
|
unsigned int ticks, ticksLast=0;
|
unsigned int ticks, ticksLast=0;
|
int ticksWait=50;
|
|
IPFrame *ethFrame=NULL;
|
IPFrame *ethFrame=NULL;
|
(void)arg;
|
(void)arg;
|
|
|
for(;;)
|
for(;;)
|
{
|
{
|
|
OS_ThreadSleep(1); //give TCP/IP stack CPU time
|
OS_InterruptMaskSet(IRQ_ETHERNET_RECEIVE); //enable interrupt
|
OS_InterruptMaskSet(IRQ_ETHERNET_RECEIVE); //enable interrupt
|
rc = OS_SemaphorePend(SemEthernet, ticksWait); //wait for interrupt
|
OS_SemaphorePend(SemEthernet, 50); //wait for interrupt
|
if(rc)
|
|
ticksWait = 50;
|
|
else
|
|
ticksWait = 2;
|
|
|
|
//Process all received packets
|
//Process all received packets
|
for(;;)
|
for(;;)
|
{
|
{
|
if(ethFrame == NULL)
|
if(ethFrame == NULL)
|
ethFrame = IPFrameGet(FRAME_COUNT_RCV);
|
ethFrame = IPFrameGet(FRAME_COUNT_RCV);
|
if(ethFrame == NULL)
|
if(ethFrame == NULL)
|
break;
|
break;
|
length = EthernetReceive(ethFrame->packet, PACKET_SIZE);
|
length = EthernetReceive(ethFrame->packet, PACKET_SIZE);
|
if(length == 0)
|
if(length == 0)
|
break;
|
break; //no packet found
|
|
if(length < 0)
|
|
continue; //CRC didn't match; process next packet
|
Led(1, 1);
|
Led(1, 1);
|
rc = IPProcessEthernetPacket(ethFrame, length);
|
rc = IPProcessEthernetPacket(ethFrame, length);
|
Led(1, 0);
|
Led(1, 0);
|
if(rc)
|
if(rc)
|
ethFrame = NULL;
|
ethFrame = NULL;
|