⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_rtl8019.c

📁 rt8019的驱动程序源代码和驱动测试程序源代码
💻 C
字号:

#include "pic.h"
#include "rs232.h"
#include "RTL8019.h"

// Local functions
static void WaitDMAComplete(void);

// ********************************************************
// 
//  Wait for DMA complete interrupt...
//
// ********************************************************
static void WaitDMAComplete(void)
{
  unsigned int i = 0;
    
  RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_RD2);

  // Check DMA complete flag, retry ONLY(!) 60000 times...
  for(i=0;i<60000;i++){
    if(RTL8019RD(ETH_PG0_ISR) & ETH_ISR_RDC){
      break;      
    }
  }
        
  RTL8019WR(ETH_PG0_ISR, ETH_ISR_RDC);
}

// ********************************************************
// 
//  Test - Send Arp request...
//
// ********************************************************
void EthernetSend(void)
{
  int i  = 0;
  int j  = 0;
  unsigned int sz = 60;
  unsigned int count = 0;
  
  // 
  unsigned char EthDestAddr[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  unsigned char EthType[2] = {0x08,0x06};
  
  unsigned char ArpReq[28] = {
  0x00,0x01, // Hardware type
  0x08,0x00, // Protocol type
  0x06,      // Hardware address length
  0x04,      // Protocol address length
  0x00,0x01, // Operation - Arp request!
  0x00,0x3B,0xDB,0xE8,0xC7,0x83, // OWN ETH ADDR
  0xC0,0xA8,0x0F,0x3A,           // Gandalf - Own IP address
  0x00,0x00,0x00,0x00,0x00,0x00, // Dest Eth address
  0xC0,0xA8,0x0F,0x01            // Dest IP (blulnt01)
  };
  
  // Set remote DM byte count and start address
  RTL8019WR(ETH_PG0_RSAR0, 0);
  RTL8019WR(ETH_PG0_RSAR1, 0x40); // Start address of remote DMA
  RTL8019WR(ETH_PG0_RBCR0, sz);
  RTL8019WR(ETH_PG0_RBCR1, sz >> 8);
      
  RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_RD1);
        
  // Transfer ETHERNET HEADER
  for(i=0;i<6;i++){
    RTL8019WR(ETH_IOPORT, EthDestAddr[i]);
  }  
  for(i=0;i<6;i++){
    RTL8019WR(ETH_IOPORT, ArpReq[8+i]);
  }
  for(i=0;i<2;i++){
    RTL8019WR(ETH_IOPORT, EthType[i]);
  }

  // Arp - Message 
  for(i=0;i<28;i++){
    RTL8019WR(ETH_IOPORT, ArpReq[i]);
  }
  
  // Padding
  for(i=0;i<18;i++){
    RTL8019WR(ETH_IOPORT,0x00);
  }
  
  // DMA complete
  WaitDMAComplete();
  
  // Set send register - send data...
  RTL8019WR(ETH_PG0_TBCR0, sz);
  RTL8019WR(ETH_PG0_TBCR1, sz >> 8);   
  RTL8019WR(ETH_PG0_TPSR, 0x40); // Transmitt start page
  RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_TXP | ETH_CR_RD2);
}

// ********************************************************
// 
//  Write to RTL8019AS chip...
//
// ********************************************************
void RTL8019WR(unsigned char Address, unsigned char data)
{
  // Kick watch-dog!
  RB7 = !RB7;
    
  // Write data setup rising edge of WRITE= 10 ns.
  // Write data hold from rising edge of WRITE=10 ns.
  PORTD = data;
  TRISD = 0x00; // Set data bus pins as outputs
  PORTA = Address & 0x1f; // Set address output, mask bit 0-4

  WRITE  = 0;    
  WRITE  = 1;
  TRISD=0xFF;  // Set data bus pins as inputs
}

// ********************************************************
// 
//  Read from RTL8019AS chip...
//
// ********************************************************
unsigned char RTL8019RD(unsigned char Address)
{    
  // Read data valid from falling edge of READ= 0-60 ns.
  // Read data hold from rising edge of READ=10 ns.
  unsigned char data = 0;
  
  // Kick watch-dog!
  RB7 = !RB7;
  
  TRISD  = 0xFF; // Set data bus pins as inputs
  PORTA  = Address & 0x1f;  // Set address output, mask bit 0-4

  READ   = 0;
  data   = PORTD;
  READ   = 1;

  return data;
}

// ********************************************************
// 
//  Check some regs...
//
// ********************************************************
void ReadRegs(void)
{
  SendRS232('I',RTL8019RD(ETH_PG0_ISR));

  SendRS232('+',RTL8019RD(ETH_PG0_CRDA0));
  SendRS232('+',RTL8019RD(ETH_PG0_CRDA1));
  SendRS232('#',RTL8019RD(ETH_PG0_CNTR0));
  SendRS232('#',RTL8019RD(ETH_PG0_CNTR1));
  SendRS232('#',RTL8019RD(ETH_PG0_CNTR2));

  RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_RD2 | ETH_CR_PS0 | ETH_CR_PS1); // Page3
  SendRS232(':',RTL8019RD(ETH_PG3_CONFIG0));
  SendRS232(':',RTL8019RD(ETH_PG3_CONFIG1));
  SendRS232(':',RTL8019RD(ETH_PG3_CONFIG2));
  SendRS232(':',RTL8019RD(ETH_PG3_CONFIG3));
  SendRS232(':',RTL8019RD(ETH_PG3_CONFIG4));

  RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_RD2); // PAGE0  
}

// ********************************************************
// 
//  TEST - Initiate RTL8019AS chip...
//
// ********************************************************
void EthInitRTL8019()
{
  int i = 0;
  int j = 0;
  unsigned int Data = 0;
  
  IOCHRDY = 1;

  // Inactivate chip---
  RTL8019WR(ETH_CR, ETH_CR_STP | ETH_CR_RD2);
  // Delay for a while...
  for(i=0;i<10000;i++){
    for(j=0;j<500;j++){
      RB7 = !RB7; // Kick watch-dog...
    }
  }  

  RTL8019WR(ETH_RESET, 0xFF);
  // Let chip restart, initiate procedure 1sec, wait a while---
  for(i=0;i<10000;i++){
    for(j=0;j<500;j++){
      RB7 = !RB7; // Kick watch-dog...
    }
  }  

  // Check if chip reset, if NOT software reset again...
  if(!(RTL8019RD(ETH_PG0_ISR) & ETH_ISR_RST)){
    RTL8019WR(ETH_RESET, RTL8019RD(ETH_RESET));

    // Let chip restart, initiate procedure <=2ms.
    for(i=0;i<10000;i++){
      for(j=0;j<100;j++){
        RB7 = !RB7; // Kick watch-dog...
        if(RTL8019RD(ETH_PG0_ISR) & ETH_ISR_RST){
          break;
        }
      }
    }
  }

  // Switch media type (initial from EEPROM?) to TP link test enable
  RTL8019WR(ETH_CR, ETH_CR_STP | ETH_CR_RD2 | ETH_CR_PS0 | ETH_CR_PS1); // Page3
  RTL8019WR(ETH_PG3_EECR, ETH_EECR_EEM0 | ETH_EECR_EEM1); // Write enable...
  RTL8019WR(ETH_PG3_CONFIG2, ETH_CONFIG2_BSELB); // Enable TP link test 
  RTL8019WR(ETH_PG3_EECR, 0); // Write disable...

  RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_RD2); // Start page0, dma abort...
  // Wait...
  for(i=0;i<10000;i++){
    for(j=0;j<100;j++){
      RB7 = !RB7; // Kick watch-dog...
    }
  }  
  
  // Set mode - ? - 8bits...
  RTL8019WR(ETH_PG0_DCR, ETH_DCR_LS | ETH_DCR_FT1);
  
  // Read DMA!!!
  RTL8019WR(ETH_PG0_RBCR0, 12);
  RTL8019WR(ETH_PG0_RBCR1, 0);
  RTL8019WR(ETH_PG0_RSAR0, 0);
  RTL8019WR(ETH_PG0_RSAR1, 0);
  
  RTL8019WR(ETH_CR, 0x08); // Start DMA remote read
  // Wait...
  for(i=0;i<10000;i++){
    for(j=0;j<50;j++){
      RB7 = !RB7; // Kick watch-dog...
    }
  }  
  
  // READ MAC Address loaded from EEPROM!!!
  for(i=0;i<14;i++){
    SendRS232(':',RTL8019RD(ETH_IOPORT));
  }

  RTL8019WR(ETH_CR, 0x61);
  // Wait...
  for(i=0;i<10000;i++){
    for(j=0;j<100;j++){
      RB7 = !RB7; // Kick watch-dog...
    }
  }  

  RTL8019WR(ETH_CR, ETH_CR_STP | ETH_CR_RD2); // Page0
  
  RTL8019WR(ETH_PG0_RBCR0, 0);
  RTL8019WR(ETH_PG0_RBCR1, 0);
  
  RTL8019WR(ETH_PG0_RCR, ETH_RCR_MON);
  
  RTL8019WR(ETH_PG0_TCR, ETH_TCR_LB0);
  
  RTL8019WR(ETH_PG0_TPSR, 0x40);
  // Receive... FIFO ring...
  RTL8019WR(ETH_PG0_BNRY, 0x4c);
  RTL8019WR(ETH_PG0_PSTART, 0x4c);
  RTL8019WR(ETH_PG0_PSTOP, 0x60);

  RTL8019WR(ETH_PG0_IMR,0);
  RTL8019WR(ETH_PG0_ISR,0xff);

  RTL8019WR(ETH_CR, ETH_CR_STP | ETH_CR_RD2 | ETH_CR_PS0); // Page1

  // Write MAC
  for(i=0;i<6;i++){
    RTL8019WR(ETH_PG1_PAR0+i, MACAddr[i]);
  }
  // Write MCast...
  for(i=0;i<8;i++){
    RTL8019WR(ETH_PG1_MAR0+i, 0x00);
  }
    
  // Set current page...
  RTL8019WR(ETH_PG1_CURR,0x4c);

  RTL8019WR(ETH_CR, ETH_CR_STP | ETH_CR_RD2); // Page0

  RTL8019WR(ETH_PG0_RCR, ETH_RCR_AB);

  RTL8019WR(ETH_PG0_ISR, 0xff);
  RTL8019WR(ETH_PG0_IMR, ETH_IMR_PRXE | ETH_IMR_PTXE);
  
  RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_RD2);
  RTL8019WR(ETH_PG0_TCR, 0);
}

// ********************************************************
// 
//  Will fetch packet from remote DMA - received packet
//
// ********************************************************
void FetchPacket(void)
{
  unsigned char recBoundary           = 0;
  unsigned char recCurrentPage        = 0;
  unsigned char recDataByteCount      = 0;
  unsigned char NextPage              = 0;
  unsigned char packageHeaderStatus   = 0; // Contents of RSR-register
  unsigned char packageHeaderNextPage = 0; // Page for next package
  unsigned short packageHeaderSize    = 0; // Size of header and packet in octets ??
  int i = 0;

  // Update global counter...
  receivedPackets--;
  
  // Get next user read packet page 
  recBoundary=RTL8019RD(ETH_PG0_BNRY)+1;
  if(recBoundary>=ETH_STOP_PAGE){
    recBoundary=ETH_FIRST_RX_PAGE;
  }
  SendRS232('#',recBoundary);
  
  RTL8019WR(ETH_CR,ETH_CR_STA | ETH_CR_RD2 | ETH_CR_PS0); // Set reg page 1
  // Read current page register (pointer to first recieive buffer page).
  recCurrentPage=RTL8019RD(ETH_PG1_CURR);    // Next receive packet page
  SendRS232('#',recCurrentPage);
  RTL8019WR(ETH_CR,ETH_CR_STA | ETH_CR_RD2); // Set reg page 0
  if(recBoundary==recCurrentPage){ 
    SendRS232('N',0x00); // No package to read
    return;
  }
  //Set remote DMA byte and start address
  RTL8019WR(ETH_PG0_RBCR0, 4);                     // Remote DMA count byte register LSB
  RTL8019WR(ETH_PG0_RBCR1, 0);                     // Remote DMA count byte register MSB
  RTL8019WR(ETH_PG0_RSAR0, 0);                     // Remote DMA start address register LSB
  RTL8019WR(ETH_PG0_RSAR1,recBoundary);            // Remote DMA start address register MSB

  // Read from buffer
  RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_RD0);   // Set remote read, Set reg page 0

  packageHeaderStatus   = RTL8019RD(ETH_IOPORT);
  SendRS232('#', packageHeaderStatus);
  packageHeaderNextPage = RTL8019RD(ETH_IOPORT);
  SendRS232('#', packageHeaderNextPage);
  packageHeaderSize     = RTL8019RD(ETH_IOPORT) << 8;
  SendRS232('#', (unsigned char)(packageHeaderSize >> 8));
  packageHeaderSize     = packageHeaderSize | RTL8019RD(ETH_IOPORT);
  SendRS232('#', (unsigned char)(packageHeaderSize));

  WaitDMAComplete();

  recDataByteCount = packageHeaderSize-4;
  SendRS232('#',(unsigned char)(recDataByteCount >> 8));
  SendRS232('#',(unsigned char)(recDataByteCount));

  // Some error...
  if(/*packageHeaderNextPage<ETH_FIRST_RX_PAGE || packageHeaderNextPage>=ETH_STOP_PAGE ||*/
     recDataByteCount<42 || recDataByteCount > 1518){ // Some wierd...
    RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_RD2 | ETH_CR_PS0);
    recCurrentPage = RTL8019RD(ETH_PG1_CURR);
    RTL8019WR(ETH_CR, ETH_CR_STA | ETH_CR_RD2);
    recBoundary = recCurrentPage-1;
    if(recBoundary<ETH_FIRST_RX_PAGE){
      recBoundary = ETH_STOP_PAGE-1;
    }
    RTL8019WR(ETH_PG0_BNRY, recBoundary);
    return;
  }

  // Start reading after the packet header
  RTL8019WR(ETH_PG0_RBCR0,(unsigned char)(recDataByteCount));      // Remote DMA count byte register LSB
  RTL8019WR(ETH_PG0_RBCR1,(unsigned char)(recDataByteCount >> 8)); // Remote DMA count byte register MSB
  RTL8019WR(ETH_PG0_RSAR0,4); //Ignore packetheader;        // Remote DMA start address register LSB
  RTL8019WR(ETH_PG0_RSAR1,recBoundary);                  // Remote DMA start address register MSB

  // Read data from DMA channel
  RTL8019WR(ETH_CR,ETH_CR_STA | ETH_CR_RD0);   // Set remote read, Set reg page 0
  for(i=0;i<recDataByteCount;i++){
    SendRS232('>',RTL8019RD(ETH_IOPORT));
  }
  WaitDMAComplete();

  NextPage=packageHeaderNextPage-1;  
  if(NextPage<ETH_FIRST_RX_PAGE){
    NextPage=ETH_STOP_PAGE-1;
  }
  RTL8019WR(ETH_PG0_BNRY,NextPage); // Page where this packet was retrieved
}

// ********************************************************
// 
//  Interrupt routine for RTL8019AS chip interrupt on line 0
//
// ********************************************************
void RTL8019_Interrupt(void)
{
  unsigned char isrData = 0;

  isrData = INT_RTL8019RD(ETH_PG0_ISR);
  ISendRS232('I',isrData);
    
  // Package received with no errors
  if(isrData & ETH_ISR_PRX){
    // Handle receive buffer indication
    ISendRS232('R', INT_RTL8019RD(ETH_PG0_RSR));
    receivedPackets++;
  }
  // Package transmitted with no errors
  if(isrData & ETH_ISR_PTX){
    // Handle transmit buffer indication
    //ISendRS232('.',0x02);
    ISendRS232('T', INT_RTL8019RD(ETH_PG0_TSR));
  }
 
  if(isrData & ETH_ISR_RST){
    ISendRS232('.',0x04);
  }
  
  if(isrData & ETH_ISR_RDC){
    ISendRS232('.',0x08);
  }  

  // Clear interrupt status register RDC
  INT_RTL8019WR(ETH_PG0_ISR, 0xff);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -