?? 8019.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 + -