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

📄 8019.c

📁 44b0下的8019驱动,查询方式,可放入lwip移植用的,转至small.box大虾
💻 C
字号:
/*
**************************************************************************************************************
*      Copyright (c) 2006 - 2010 Small.Box Corp. All rights reserved.                                             
*                                          
* FILENAME
*   RTL8019.c
*
* VERSION
*   V1.00
*
* HISTORY
*   2006/09/27      Ver 1.0 Created by Small.Box 
*   2006/10/17      Ver 1.1 目前以太网驱动运行【发送、接收】稳定
* 
* REMARK
*   None     
* 
*
**************************************************************************************************************
*/


#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include "RTL8019.h"

#include "includes.h"

#include "opt.h"


#define RPSTART 0x4c


#ifdef RTL8019_OP_16
#define RPSTOP 0x80
#else
#define RPSTOP 0x60
#endif

#define SPSTART 0x40


static U8 rBNRY;
static U8 SrcMacID[ETH_ALEN] = {0x00,0x50,0xba,0x33,0xbe,0x44};


/*
*******************************************************************************
*        RTL8019IOInit
*
*Description: 初始化I/O口
*
*Arguments : 无
*    
*Returns : 无
*
*Note  : V1.0 by Small.Box at 2006.09.27
*
*******************************************************************************
*/

void RTL8019IOInit(void)
{

    //这里初始化ARM与RTL8019连接的I/O口,由移植人,自己添加,这里不在上传我的代码

}

 

/*
*******************************************************************************
*        SetRegPage
*
*Description: 设置8019选中页地址
*
*Arguments : PageIdx 选中第几页【0.1.2.3】
*    
*
*Returns : 无
*
*Note  : V1.0 by Small.Box at 2006.09.27
*    
*    PageIdx = 0 -> temp = 0x21
*    PageIdx = 1 -> temp = 0x61
*    PageIdx = 2 -> temp = 0xA1
*    PageIdx = 3 -> temp = 0xE1
*
*******************************************************************************
*/

void SetRegPage(U8 PageIdx)
{
 U8 temp;
 
 temp = inportb(BaseAddr); 
 temp = (temp&0x3b)|(PageIdx<<6);      
 outportb(temp, BaseAddr);
}


/*
*******************************************************************************
*        SetMacID
*
*Description: 设置8019物理MAC地址
*
*Arguments : 无
*    
*
*Returns : 无
*
*Note  : V1.0 by Small.Box at 2006.09.27
*
*******************************************************************************
*/

static void SetMacID()
{ 
 SetRegPage(1);
 outportb(SrcMacID[0], PAR0);
 outportb(SrcMacID[1], PAR1);
 outportb(SrcMacID[2], PAR2);
 outportb(SrcMacID[3], PAR3);
 outportb(SrcMacID[4], PAR4);
 outportb(SrcMacID[5], PAR5);
}


/*
*******************************************************************************
*        Rst8019
*
*Description: 8019软复位
*
*Arguments : 无
*    
*
*Returns : 无
*
*Note  : V1.0 by Small.Boxat 2006.09.27
*
*******************************************************************************
*/

static U8 Rst8019()
{
 int i;
 
 outportb(0x5a, RstAddr);
 //需要添加一个延时代码,为了确保复位指令正常运行
 SetRegPage(0);
 return (inportb(ISR)&0x80);
}


/*
*******************************************************************************
*        WakeRtl8019as
*
*Description: 设置8019第三页寄存器
*
*Arguments : 无
*    
*
*Returns : 无
*
*Note  : V1.0 by Small.Box at 2006.10.09
*    
*
*******************************************************************************
*/

static void WakeRtl8019as()
{
 SetRegPage(3); 
 outportb(0xcf, CR9346);  //set eem1-0, 11 ,enable write config register
 outportb(0x70, CONFIG3); //clear pwrdn, sleep mode, set led0 as led_link, led1 as led_rx 
 outportb(0x3f, CR9346);  //disable write config register
}


/*
*******************************************************************************
*        InitRS8019
*
*Description: RTL8019AS初始化
*
*Arguments : 无
*    
*
*Returns : 无
*
*Note  : V1.0 by Small.Box at 2006.10.09
*    
*
*******************************************************************************
*/

static void InitRS8019()
{
 int i;
 

 outportb(0x21, BaseAddr);
 for(i=0;i<1000;i++);  //延时确保芯片处于停止模式

 outportb(RPSTART, Pstart);
 outportb(RPSTOP, Pstop);
 outportb(RPSTART, BNRY);
      
 outportb(SPSTART, TPSR);
 outportb(0xcc, RCR);
 outportb(0xe0, TCR);
#ifdef RTL8019_OP_16
 outportb(0xc9, DCR);  // set DCR 0xc9, 16bit DMA 
#else
 outportb(0xc8, DCR);  // 8bit DMA
#endif
 outportb(0x03, IMR);  // set IMR 0x03
 outportb(0xff, ISR); 
 
 SetRegPage(1); 
 outportb(RPSTART+1, CURR);
 
 outportb(0x00, MAR0);
 outportb(0x00, MAR1);
 outportb(0x00, MAR2);
 outportb(0x00, MAR3);
 outportb(0x00, MAR4);
 outportb(0x00, MAR5);
 outportb(0x00, MAR6);
 outportb(0x00, MAR7);
  
 outportb(0x22, BaseAddr); // set page 0 and start
  rBNRY = RPSTART;
 
}

/*
*******************************************************************************
*        board_eth_init
*
*Description: RTL8019AS初始化
*    [1]初始化arm I/O口
*    [2]复位rtl8019
*    [3]初始化rtl8019寄存器
*    [4]设置rtl8019物理地址MAC
*
*
*Arguments : 无
*    
*
*Returns : (0)
*
*Note  : V1.0 by Small.Box at 2006.10.09
*    
*
*******************************************************************************
*/

int board_eth_init(void)
{ 
 int i;
 
 RTL8019IOInit();


 //WakeRtl8019as();
 
 if(!Rst8019()) {
  //复位失败
  return -1;
  
 } else {
  //复位成功
 }
 
 InitRS8019();
 
 i  = inportb(ID8019L); 
 i |= inportb(ID8019H)<<8;
 
 SetMacID();
 
 return 0;
}

/*
*******************************************************************************
*        board_eth_send
*
*Description: RTL8019AS发送驱动程序
*
*Arguments : *data 发送的数据
*    *len 发送数据的长度
*    
*Returns : (0)  异常
* 
*
*Note  : 页 0x40 ~ 0x4b 为发送缓冲区 共计大小12页
*    以太网最大的一个数据包是1514字节+4字节校验.
*    一个最大的数据包需要6页=256*6=1536字节.
*    12页可以放两个最大的包.我们把前6页0x40--0x45称为发送缓冲1,
*    接下来的6页0x46--0x4B称为发送缓冲2.
*
*    V1.0 by Small.Boxat 2006.10.09   存在只能发送256个数据的bug
*    V1.1 Modify By Small.Box at 2006.10.17  修改了只能发送256个数据的bug
*    目前运行稳定
*******************************************************************************
*/

int board_eth_send(unsigned char *data, unsigned short len)
{
 static sFlag = 0;
 int i;
 U8 send_page;
 
#if 0 
 LWIP_DEBUGF(RTL8019_DEBUG, ("*****************************\r\n"));
 LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's Send len    = %d   \r\n",len));
 LWIP_DEBUGF(RTL8019_DEBUG, ("*****************************\r\n"));
#endif
 
 send_page  = SPSTART;   
 send_page += (sFlag&1)?6:0; 
 sFlag++;      //Modify by Small.Box at 06.10.17 13:11pm
  
 if(len<60) {
  for(; len<60; len++)
   data[len] = 0x20;
 }
 
 SetRegPage(0);
 outportb(0x22, BaseAddr);
 outportb(0, RSAR0);
 outportb(send_page, RSAR1);
 
 outportb((unsigned char)len&0xff, RBCR0);
 outportb((unsigned char)(len>>8), RBCR1);

 outportb(0x12, BaseAddr);
 
#ifdef RTL8019_OP_16
 len += len&1;
 for(i=0; i<(len>>1); i++)
  outportb(((U16 *)data)[i], RWPORT);
#else 
 for(i=0; i<len; i++)
  outportb(data[i], RWPORT); 
#endif  

 while(inportb(BaseAddr)&4);
 outportb(send_page, TPSR);

 outportb((unsigned char)len&0xff, TBCR0);
 outportb((unsigned char)(len>>8), TBCR1);
  
 outportb(0x1e, BaseAddr);
 return 0;
}


/*
*******************************************************************************
*        board_eth_rcv
*
*Description: RTL8019AS接收驱动程序
*
*Arguments : *data 接收的数据
*    *len 接收数据的长度
*    
*Returns : (0)  异常 【复位或溢出】 
*    (1)  正常
*    (-1) 接收失败
*
*Note  : V1.0 by Small.Box at 2006.10.09
*    V1.1 Modify by Small.Box at 2006.10.17 修改了只能接收255个数据的bug
*
*******************************************************************************
*/

int board_eth_rcv(unsigned char *data, unsigned int *len)
{ U8 RxPageBeg, RxPageEnd;
 U8 RxNextPage;
 U8 RxStatus;
 int i, isrFlag,RxLength;
 U16 *data_16;
 //int RxStatusJust;

 U8 RxCntH,RxCntL;

 data_16 = (U16 *)data;
 
 SetRegPage(0);
  outportb(rBNRY, BNRY);  //rBNRY = RPSTART = 0x4c 
  
 isrFlag = inportb(ISR);   
 if((isrFlag & 0x90) != 0) { 
  Printf("\r\n------Rtl8019 Reseting or Overflow!------\r\n");
  InitRS8019();
  return (0);
 }
 else if(isrFlag & 1) {   //接收成功
  //Printf("\r\n------Rtl8019 Receive Successfully!------\r\n");
  outportb(0x1, ISR);   //清除中断标志
 } else {
  //Printf("\r\n------Rtl8019 Receive Failed!------\r\n");
  return (-1);
 }  

 SetRegPage(1);
 RxPageEnd = inportb(CURR);
 LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's CURR = %d \r\n",RxPageEnd));
 
 SetRegPage(0);

 RxPageBeg = rBNRY+1;
 if(RxPageBeg>=RPSTOP)
  RxPageBeg = RPSTART;
 
 outportb(0x22, BaseAddr);

 outportb(0, RSAR0); 
 outportb(RxPageBeg, RSAR1);
 outportb(4, RBCR0); 
 outportb(0, RBCR1);
 outportb(0xa, BaseAddr);
 
#ifdef RTL8019_OP_16
 RxLength   = inportw(RWPORT);
 RxStatus   = RxLength&0xff;
 RxNextPage = RxLength>>8;
 RxLength   = inportw(RWPORT);
 RxLength  += RxLength&1;
#else
 RxStatus   = inportb(RWPORT);
 RxNextPage = inportb(RWPORT); 
 RxLength   = inportb(RWPORT);
 RxCntH    = inportb(RWPORT);
 RxLength  |= ((unsigned short)RxCntH) << 8;
 *len    = RxLength;
#endif

#if 0
 LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's RxStatus  = %d \r\n",RxStatus));
 LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's RxNextPage  = %d \r\n",RxNextPage));
 LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's RxLength  = %d \r\n",RxLength));  
 LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's RxPageBeg  = %d \r\n",RxPageBeg));
#endif
 
 if(RxLength > ETH_FRAME_LEN) {
  if(RxPageEnd==RPSTART)
   rBNRY = RPSTOP-1;
  else
   rBNRY = RxPageEnd-1;
  outportb(rBNRY, BNRY);
  LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's Rev Length --->ERROR<---\r\n"));
  return -1;
 }
 
 RxCntH = (unsigned char)(RxLength>>8);
 RxCntL = (unsigned char)(RxLength & 0x00ff);

 LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's Rev RxCntH = %d\r\n",RxCntH));
 LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's Rev RxCntL = %d\r\n",RxCntL));

 outportb(4, RSAR0); 
 outportb(RxPageBeg, RSAR1); 
 outportb(RxCntL, RBCR0);
 outportb(RxCntH, RBCR1); 
 outportb(0xa, BaseAddr);
 
#ifdef RTL8019_OP_16
 i = 2;
 data_16 -= i;
 RxLength >>= 1;
 for(; RxLength--;) {
  if(!(i&0x7f)) {
   outportb(RxPageBeg, BNRY);
    RxPageBeg++;
    if(RxPageBeg>=RPSTOP)
     RxPageBeg = RPSTART;
  }
  data_16[i++] = inportw(RWPORT);
 } 
#else
  for(; RxLength--;) {
   
   if(!((unsigned char)i&0xff)) {
    outportb(RxPageBeg, BNRY);
    RxPageBeg++;
    if(RxPageBeg>=RPSTOP)
     RxPageBeg = RPSTART;
    
    LWIP_DEBUGF(RTL8019_DEBUG, ("RTL8019's RxPageBeg(1) = %d \r\n",RxPageBeg));          
   }
   
   *data = inportb(RWPORT);
   data++;

  }
#endif
 
 outportb(RxPageBeg, BNRY);
 rBNRY = RxPageBeg;
 
 return 1;
}

⌨️ 快捷键说明

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