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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [ethernet.c] - Diff between revs 418 and 422

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 418 Rev 422
Line 23... Line 23...
#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 16           //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);
 
 
 
static unsigned char gDestMac[]={0x5d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
static unsigned char gDestMac[]={0x5d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
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;
Line 161... Line 159...
   for(i = 0; i < 10000; ++i)
   for(i = 0; i < 10000; ++i)
   {
   {
      if(MemoryRead(IRQ_STATUS) & IRQ_ETHERNET_TRANSMIT)
      if(MemoryRead(IRQ_STATUS) & IRQ_ETHERNET_TRANSMIT)
         break;
         break;
   }
   }
   //if(i > 100)
 
   //   printf("wait=%d ", i);
 
 
 
   Led(2, 2);
   Led(2, 2);
   while(length < 60 || (length & 3) != 0)
   while(length < 60 || (length & 3) != 0)
      buffer[length++] = 0;
      buffer[length++] = 0;
 
 
Line 210... Line 206...
   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
      OS_SemaphorePend(SemEthernet, 50);            //wait for interrupt
      OS_SemaphorePend(SemEthernet, 50);            //wait for interrupt
 
 
      //Process all received packets
      //Process all received packets
      for(;;)
      for(;;)
Line 306... Line 301...
   while(*(volatile int*)COUNTER_REG - value < 0)
   while(*(volatile int*)COUNTER_REG - value < 0)
      ;
      ;
}
}
 
 
 
 
void EthernetInit(unsigned char MacAddress[6])
//Use the Ethernet MDIO bus to configure the Ethernet PHY
 
static int EthernetConfigure(int index, int value)
{
{
   //Format of SMI data: 0101 A4:A0 R4:R0 00 D15:D0
   unsigned int data;
   unsigned long data=0x5f800100; //SMI R0 = 10Mbps full duplex
   int i, bit, rc=0;
   //unsigned long data=0x5f800000; //SMI R0 = 10Mbps half duplex
 
   int i, value;
 
   volatile unsigned char *buf = (unsigned char*)ETHERNET_RECEIVE;
 
 
 
   CrcInit();
   //Format of SMI data: 0101 A4:A0 R4:R0 00 D15:D0
   if(MacAddress)
   if(value <= 0xffff)
   {
      data = 0x5f800000;  //write
      for(i = 0; i < 6; ++i)
   else
      {
      data = 0x6f800000;  //read
         value = MacAddress[i];
   data |= index << 18 | value;
         gDestMac[i+1] = (unsigned char)((value >> 4) | (value << 4));
 
      }
 
   }
 
 
 
   //Configure Ethernet PHY for 10Mbps full duplex via SMI interface
   MemoryWrite(GPIO0_SET, ETHERNET_MDIO | ETHERNET_MDIO_WE | ETHERNET_MDC);
   MemoryWrite(GPIO0_OUT, ETHERNET_MDIO | ETHERNET_MDIO_WE | ETHERENT_MDC);
 
   for(i = 0; i < 34; ++i)
   for(i = 0; i < 34; ++i)
   {
   {
      MemoryWrite(GPIO0_OUT, ETHERENT_MDC);    //clock high
      MemoryWrite(GPIO0_SET, ETHERNET_MDC);    //clock high
      SpinWait(10);
      SpinWait(10);
      MemoryWrite(GPIO0_CLEAR, ETHERENT_MDC);  //clock low
      MemoryWrite(GPIO0_CLEAR, ETHERNET_MDC);  //clock low
      SpinWait(10);
      SpinWait(10);
   }
   }
   for(i = 31; i >= 0; --i)
   for(i = 31; i >= 0; --i)
   {
   {
      value = (data >> i) & 1;
      bit = (data >> i) & 1;
      if(value)
      if(bit)
         MemoryWrite(GPIO0_OUT, ETHERNET_MDIO);
         MemoryWrite(GPIO0_SET, ETHERNET_MDIO);
      else
      else
         MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO);
         MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO);
      MemoryWrite(GPIO0_OUT, ETHERENT_MDC);    //clock high
 
      SpinWait(10);
      SpinWait(10);
      MemoryWrite(GPIO0_CLEAR, ETHERENT_MDC);  //clock low
      MemoryWrite(GPIO0_SET, ETHERNET_MDC);    //clock high
      SpinWait(10);
      SpinWait(10);
 
      rc = rc << 1 | ((MemoryRead(GPIOA_IN) >> 13) & 1);
 
      MemoryWrite(GPIO0_CLEAR, ETHERNET_MDC);  //clock low
 
      SpinWait(10);
 
      if(value > 0xffff && i == 17)
 
         MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO_WE);
 
   }
 
   MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO | ETHERNET_MDIO_WE | ETHERNET_MDC);
 
   return (rc >> 1) & 0xffff;
   }
   }
   MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO_WE | ETHERNET_ENABLE);
 
 
 
 
void EthernetInit(unsigned char MacAddress[6])
 
{
 
   int i, value;
 
   volatile unsigned char *buf = (unsigned char*)ETHERNET_RECEIVE;
 
 
 
   CrcInit();
 
   if(MacAddress)
 
   {
 
      for(i = 0; i < 6; ++i)
 
      {
 
         value = MacAddress[i];
 
         gDestMac[i+1] = (unsigned char)((value >> 4) | (value << 4));
 
      }
 
   }
 
 
 
   EthernetConfigure(4, 0x0061);        //advertise 10Base-T full duplex
 
   EthernetConfigure(0, 0x1300);        //start auto negotiation
 
#if 0
 
   OS_ThreadSleep(100);
 
   printf("reg4=0x%x (0x61)\n", EthernetConfigure(4, 0x10000));
 
   printf("reg0=0x%x (0x1100)\n", EthernetConfigure(0, 0x10000));
 
   printf("reg1=status=0x%x (0x7809)\n", EthernetConfigure(1, 0x10000));
 
   printf("reg5=partner=0x%x (0x01e1)\n", EthernetConfigure(5, 0x10000));
 
#endif
 
 
 
   MemoryWrite(GPIO0_CLEAR, ETHERNET_ENABLE);
 
 
   //Clear receive buffer
   //Clear receive buffer
   for(i = 0; i <= INDEX_MASK; ++i)
   for(i = 0; i <= INDEX_MASK; ++i)
      buf[i] = BYTE_EMPTY;
      buf[i] = BYTE_EMPTY;
 
 
Line 362... Line 385...
 
 
   //Setup interrupts for receiving data
   //Setup interrupts for receiving data
   OS_InterruptRegister(IRQ_ETHERNET_RECEIVE, EthernetIsr);
   OS_InterruptRegister(IRQ_ETHERNET_RECEIVE, EthernetIsr);
 
 
   //Start receive DMA
   //Start receive DMA
   MemoryWrite(GPIO0_OUT, ETHERNET_ENABLE);
   MemoryWrite(GPIO0_SET, ETHERNET_ENABLE);
}
}
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

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