OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [ethernet.c] - Blame information for rev 417

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 416 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: Plasma Ethernet MAC
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 1/12/08
5
 * FILENAME: ethernet.c
6
 * PROJECT: Plasma CPU core
7
 * COPYRIGHT: Software placed into the public domain by the author.
8
 *    Software 'as is' without warranty.  Author liable for nothing.
9
 * DESCRIPTION:
10
 *    Ethernet MAC implementation.
11
 *    Data is received from the Ethernet PHY four bits at a time.
12
 *    After 32-bits are received they are written to 0x13ff0000 + N.
13
 *    The data is received LSB first for each byte which requires the
14
 *    nibbles to be swapped.
15
 *    Transmit data is read from 0x13fe0000.  Write length/4+1 to
16
 *    ETHERNET_REG to start transfer.
17
 *--------------------------------------------------------------------*/
18
#include "plasma.h"
19
#include "rtos.h"
20
#include "tcpip.h"
21
 
22
#define POLYNOMIAL  0x04C11DB7   //CRC bit 33 is truncated
23
#define TOPBIT      (1<<31)
24
#define BYTE_EMPTY  0xde         //Data copied into receive buffer
25
#define COUNT_EMPTY 16           //Count to decide there isn't data
26
#define INDEX_MASK  0xffff       //Size of receive buffer
27
 
28
//void dump(const unsigned char *data, int length);
29
 
30
static unsigned char gDestMac[]={0x5d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
31
static unsigned int CrcTable[256];
32
static unsigned char reflect[256];
33
static unsigned char reflectNibble[256];
34
static OS_Semaphore_t *SemEthernet, *SemEthTransmit;
35
static int gIndex;          //byte index into 0x13ff0000 receive buffer
36
static int gCheckedBefore;
37
 
38
 
39
//Read received data from 0x13ff0000.  Data starts with 0x5d+MACaddress.
40
//Data is being received while processing the data.  Therefore,
41
//all errors require waiting and then re-processing the data
42
//to see if the error is fixed by receiving the rest of the packet.
43
int EthernetReceive(unsigned char *buffer, int length)
44
{
45
   int count;
46
   int start, i, j, shift, offset, index;
47
   int byte, byteNext;
48
   unsigned long crc;
49
   int byteCrc;
50
   volatile unsigned char *buf = (unsigned char*)ETHERNET_RECEIVE;
51
   int packetExpected;
52
 
53
   //Find the start of a frame
54
   packetExpected = MemoryRead(IRQ_STATUS) & IRQ_ETHERNET_RECEIVE;
55
   MemoryRead(ETHERNET_REG);        //clear receive interrupt
56
 
57
   //Find dest MAC address
58
   for(offset = 0; offset <= INDEX_MASK; ++offset)
59
   {
60
      index = (gIndex + offset) & INDEX_MASK;
61
      byte = buf[index];
62
      if(byte == 0x5d)  //bit pattern 01011101
63
      {
64
         for(i = 1; i < sizeof(gDestMac); ++i)
65
         {
66
            j = (index + i) & INDEX_MASK;
67
            byte = buf[j];
68
            if(byte != 0xff && byte != gDestMac[i])
69
               break;
70
         }
71
         if(i == sizeof(gDestMac))
72
            break;    //found dest MAC
73
      }
74
      else if(byte == BYTE_EMPTY && packetExpected == 0)
75
         return 0;
76
   }
77
   if(offset > INDEX_MASK)
78
      return 0;
79
   while(gIndex != index)
80
   {
81
      buf[gIndex] = BYTE_EMPTY;
82
      gIndex = (gIndex + 1) & INDEX_MASK;
83
   }
84
 
85
   //Found start of frame.  Now find end of frame and check CRC.
86
   start = gIndex;
87
   gIndex = (gIndex + 1) & INDEX_MASK;           //skip 0x5d byte
88
   crc = 0xffffffff;
89
   for(count = 0; count < length; )
90
   {
91
      byte = buf[gIndex];
92
      gIndex = (gIndex + 1) & INDEX_MASK;
93
 
94
      byte = ((byte << 4) & 0xf0) | (byte >> 4); //swap nibbles
95
      buffer[count++] = (unsigned char)byte;
96
      byte = reflect[byte] ^ (crc >> 24);        //calculate CRC32
97
      crc = CrcTable[byte] ^ (crc << 8);
98
      if(count >= 40)
99
      {
100
         //Check if CRC matches to detect end of frame
101
         byteCrc = reflectNibble[crc >> 24];
102
         byteNext = buf[gIndex];
103
         if(byteCrc == byteNext)
104
         {
105
            for(i = 1; i < 4; ++i)
106
            {
107
               shift = 24 - (i << 3);
108
               byteCrc = reflectNibble[(crc >> shift) & 0xff];
109
               byteNext = buf[(gIndex + i) & 0xffff];
110
               if(byteCrc != byteNext)
111
               {
112
                  //printf("nope %d %d 0x%x 0x%x\n", count, i, byteCrc, byteNext);
113
                  i = 99;
114
               }
115
            }
116
            if(i == 4)
117
            {
118
               //Found end of frame -- set used bytes to BYTE_EMPTY
119
               //printf("Found it! %d\n", count);
120
               gIndex = (gIndex + 4) & INDEX_MASK;
121
               for(i = 0; i < count+5; ++i)
122
                  buf[(start + i) & INDEX_MASK] = BYTE_EMPTY;
123
               while(gIndex & 3)
124
               {
125
                  buf[gIndex] = BYTE_EMPTY;
126
                  gIndex = (gIndex + 1) & INDEX_MASK;
127
               }
128
               gCheckedBefore = 0;
129
               return count;
130
            }
131
         }
132
      }
133
   }
134
   gIndex = start;
135
   if(gCheckedBefore++ > 1)
136
   {
137
      buf[gIndex] = BYTE_EMPTY;
138
      gIndex = (gIndex + 1) & INDEX_MASK;
139
   }
140
   return 0;        //wait for more data
141
}
142
 
143
 
144
//Copy transmit data to 0x13fe0000 with preamble and CRC32
145
void EthernetTransmit(unsigned char *buffer, int length)
146
{
147
   int i, byte, shift;
148
   unsigned long crc;
149
   volatile unsigned char *buf = (unsigned char*)ETHERNET_TRANSMIT;
150
 
151
   OS_SemaphorePend(SemEthTransmit, OS_WAIT_FOREVER);
152
 
153
   //Wait for previous transfer to complete
154
   for(i = 0; i < 10000; ++i)
155
   {
156
      if(MemoryRead(IRQ_STATUS) & IRQ_ETHERNET_TRANSMIT)
157
         break;
158
   }
159
   //if(i > 100)
160
   //   printf("wait=%d ", i);
161
 
162
   Led(2, 2);
163
   while(length < 60 || (length & 3) != 0)
164
      buffer[length++] = 0;
165
 
166
   //Start of Ethernet frame
167
   for(i = 0; i < 7; ++i)
168
      buf[i] = 0x55;
169
   buf[7] = 0x5d;
170
 
171
   //Calculate CRC32
172
   crc = 0xffffffff;
173
   for(i = 0; i < length; ++i)
174
   {
175
      byte = buffer[i];
176
      buf[i + 8] = (unsigned char)((byte << 4) | (byte >> 4)); //swap nibbles
177
      byte = reflect[byte] ^ (crc >> 24);        //calculate CRC32
178
      crc = CrcTable[byte] ^ (crc << 8);
179
   }
180
 
181
   //Output CRC32
182
   for(i = 0; i < 4; ++i)
183
   {
184
      shift = 24 - (i << 3);
185
      byte = reflectNibble[(crc >> shift) & 0xff];
186
      buf[length + 8 + i] = (unsigned char)byte;
187
   }
188
 
189
   //Start transfer
190
   length = (length + 12 + 4) >> 2;
191
   MemoryWrite(ETHERNET_REG, length);
192
   Led(2, 0);
193
 
194
   OS_SemaphorePost(SemEthTransmit);
195
}
196
 
197
 
198
void EthernetThread(void *arg)
199
{
200
   int length;
201 375 rhoads
   int rc;
202 416 rhoads
   unsigned int ticks, ticksLast=0;
203
   int ticksWait=50;
204 375 rhoads
   IPFrame *ethFrame=NULL;
205 416 rhoads
   (void)arg;
206
 
207
   for(;;)
208 375 rhoads
   {
209 416 rhoads
      OS_InterruptMaskSet(IRQ_ETHERNET_RECEIVE);      //enable interrupt
210
      rc = OS_SemaphorePend(SemEthernet, ticksWait);  //wait for interrupt
211
      if(rc)
212
         ticksWait = 50;
213
      else
214
         ticksWait = 2;
215
 
216
      //Process all received packets
217
      for(;;)
218
      {
219 375 rhoads
         if(ethFrame == NULL)
220 416 rhoads
            ethFrame = IPFrameGet(FRAME_COUNT_RCV);
221
         if(ethFrame == NULL)
222 375 rhoads
            break;
223 416 rhoads
         length = EthernetReceive(ethFrame->packet, PACKET_SIZE);
224 373 rhoads
         if(length == 0)
225
            break;
226 416 rhoads
         Led(1, 1);
227
         rc = IPProcessEthernetPacket(ethFrame, length);
228
         Led(1, 0);
229
         if(rc)
230
            ethFrame = NULL;
231
      }
232
 
233
      ticks = OS_ThreadTime();
234
      if(ticks - ticksLast >= 50)
235
      {
236
         IPTick();
237
         ticksLast = ticks;
238
      }
239
   }
240
}
241
 
242
 
243
void EthernetIsr(void *arg)
244
{
245 373 rhoads
   (void)arg;
246 416 rhoads
   OS_InterruptMaskClear(IRQ_ETHERNET_RECEIVE);
247 373 rhoads
   OS_SemaphorePost(SemEthernet);
248 416 rhoads
}
249
 
250
 
251
/******************* CRC32 calculations **********************
252
 * The CRC32 code is modified from Michale Barr's article in
253
 * Embedded Systems Programming January 2000.
254
 * A CRC is really modulo-2 binary division.  Substraction means XOR. */
255
static unsigned int Reflect(unsigned int value, int bits)
256
{
257
   unsigned int num=0;
258
   int i;
259
   for(i = 0; i < bits; ++i)
260
   {
261
      num = (num << 1) | (value & 1);
262
      value >>= 1;
263
   }
264
   return num;
265
}
266
 
267
 
268
static void CrcInit(void)
269
{
270
   unsigned int remainder;
271
   int dividend, bit, i;
272
 
273
   //Compute the remainder of each possible dividend
274
   for(dividend = 0; dividend < 256; ++dividend)
275
   {
276
      //Start with the dividend followed by zeros
277
      remainder = dividend << 24;
278
      //Perform modulo-2 division, a bit at a time
279
      for(bit = 8; bit > 0; --bit)
280
      {
281
         //Try to divide the current data bit
282
         if(remainder & TOPBIT)
283
            remainder = (remainder << 1) ^ POLYNOMIAL;
284
         else
285
            remainder = remainder << 1;
286
      }
287
      CrcTable[dividend] = remainder;
288
   }
289
   for(i = 0; i < 256; ++i)
290
   {
291
      reflect[i] = (unsigned char)Reflect(i, 8);
292
      reflectNibble[i] = (unsigned char)((Reflect((i >> 4) ^ 0xf, 4) << 4) |
293
         Reflect(i ^ 0xf, 4));
294
   }
295
}
296
 
297
 
298
static void SpinWait(int clocks)
299
{
300
   int value = *(volatile int*)COUNTER_REG + clocks;
301
   while(*(volatile int*)COUNTER_REG - value < 0)
302
      ;
303
}
304
 
305
 
306
void EthernetInit(unsigned char MacAddress[6])
307
{
308
   //Format of SMI data: 0101 A4:A0 R4:R0 00 D15:D0
309
   unsigned long data=0x5f800100; //SMI R0 = 10Mbps full duplex
310
   //unsigned long data=0x5f800000; //SMI R0 = 10Mbps half duplex
311
   int i, value;
312
   volatile unsigned char *buf = (unsigned char*)ETHERNET_RECEIVE;
313
 
314 375 rhoads
   CrcInit();
315
   if(MacAddress)
316 416 rhoads
   {
317
      for(i = 0; i < 6; ++i)
318
      {
319
         value = MacAddress[i];
320
         gDestMac[i+1] = (unsigned char)((value >> 4) | (value << 4));
321 375 rhoads
      }
322 416 rhoads
   }
323
 
324
   //Configure Ethernet PHY for 10Mbps full duplex via SMI interface
325
   MemoryWrite(GPIO0_OUT, ETHERNET_MDIO | ETHERNET_MDIO_WE | ETHERENT_MDC);
326
   for(i = 0; i < 34; ++i)
327
   {
328
      MemoryWrite(GPIO0_OUT, ETHERENT_MDC);    //clock high
329
      SpinWait(10);
330
      MemoryWrite(GPIO0_CLEAR, ETHERENT_MDC);  //clock low
331
      SpinWait(10);
332
   }
333
   for(i = 31; i >= 0; --i)
334
   {
335
      value = (data >> i) & 1;
336
      if(value)
337
         MemoryWrite(GPIO0_OUT, ETHERNET_MDIO);
338
      else
339
         MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO);
340
      MemoryWrite(GPIO0_OUT, ETHERENT_MDC);    //clock high
341
      SpinWait(10);
342
      MemoryWrite(GPIO0_CLEAR, ETHERENT_MDC);  //clock low
343
      SpinWait(10);
344
   }
345
   MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO_WE | ETHERNET_ENABLE);
346
 
347
   //Clear receive buffer
348
   for(i = 0; i <= INDEX_MASK; ++i)
349
      buf[i] = BYTE_EMPTY;
350
 
351 385 rhoads
   if(SemEthernet == NULL)
352
   {
353 416 rhoads
      SemEthernet = OS_SemaphoreCreate("eth", 0);
354
      SemEthTransmit = OS_SemaphoreCreate("ethT", 1);
355
      OS_ThreadCreate("eth", EthernetThread, NULL, 240, 0);
356
   }
357
 
358
   //Setup interrupts for receiving data
359 375 rhoads
   OS_InterruptRegister(IRQ_ETHERNET_RECEIVE, EthernetIsr);
360 416 rhoads
 
361
   //Start receive DMA
362
   MemoryWrite(GPIO0_OUT, ETHERNET_ENABLE);
363
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.