📄 rtl8019as(backup).c
字号:
Page(1); // 选择第1页的寄存器
Curr=IN_B(P1_CURR); // 读接收缓冲区写页指针
Page(0);
if (Curr==0)
return 0; // 读的过程出错返回0
if (++Bnry>0x7F)
Bnry=0x4C;
if (Bnry!=Curr)
{ // 此时表示有新的数据包在缓冲区里;
// 读取一包的前18个字节:4字节的8019头部,
// 6字节目的地址,6字节源地址,2字节协议
Uart_Printf("\n Count : %d\n", ++currCount);
Page(0); // 返回到页0
OUT_B(P0_RSAR1, Bnry); // 设远程(DMA)起始地址寄存器1(高字节)为Bnry
OUT_B(P0_RSAR0, 0x00); // 设远程(DMA)起始地址寄存器0(低字节)为0
OUT_B(P0_RBCR1, 0x00); // 设远程字节计数寄存器1(高字节)为0
OUT_B(P0_RBCR0, 18); // 设远程字节计数寄存器0(低字节)[准备读包头18字节]
OUT_B(P_CR, 0x0A); // 设命令寄存器:选择页0, 远程DMA读, 开始执行。
for (i=0; i<18; i++) // 读18个字节
{
Packet->Bytes.ByteBuf[i]=IN_B(P_DMA_PORT);
// Uart_Printf("%x,", Packet->Bytes.ByteBuf[i]);
}
Uart_Printf("\n");
i=Packet->Bytes.ByteBuf[17]; // 将长度字段的高低字节掉转
Packet->Bytes.ByteBuf[17]=Packet->Bytes.ByteBuf[16];
Packet->Bytes.ByteBuf[16]=i;
Packet->EtherFrame.Length -= 4; // 去掉4个字节的CRC,表示读入的数据包有效
Uart_Printf("RTL8019AS_RcvPacket Length = %d Protocal.0 = %x", Packet->EtherFrame.Length, Packet->EtherFrame.Protocal);
if(((Packet->EtherFrame.Status & 0x01) == 0)||(Packet->EtherFrame.NextPage > 0x7F)||
(Packet->EtherFrame.NextPage < 0x4C)||(Packet->EtherFrame.Length > 1514))
{ // 接收状态错误, 或者NextPageStart错误,或者长度错误, 将丢弃所有数据包
//Uart_Printf("\n RTL8019AS_RcvPacket WRONG");
Page(1); // 选页1的寄存器
Curr=IN_B(P1_CURR); // 读接收缓冲区写页指针
Page(0); // 选页0的寄存器
Bnry=Curr-1;
if (Bnry<0x4C)
Bnry=0x7F;
OUT_B(P0_BNRY, Bnry); // 写接收缓冲区读页指针
return 0;
}
else
{ // 表示数据包是完好的.读取剩下的数据
if ((Packet->EtherFrame.Protocal==0x0800)|| // IP包
(Packet->EtherFrame.Protocal==0x0806)) // ARP包
{
Uart_Printf(" RTL8019AS_RcvPacket OK\n");
Uart_Printf("%x; ", Packet->EtherFrame.Status);
Uart_Printf("%x; ", Packet->EtherFrame.NextPage);
Uart_Printf("%x; ", Packet->EtherFrame.Length);
for(i = 0; i < 3; i++)
{
Uart_Printf("%x; ", Packet->EtherFrame.DestDodeId[i]);
}
for(i = 0; i < 3; i++)
{
Uart_Printf("%x; ", Packet->EtherFrame.SourceNodeId[i]);
}
Uart_Printf("RTL8019AS_RcvPacket.Protocal = %x\n ", Packet->EtherFrame.Protocal);
for(i = 18; i < 60; i++)
{
Uart_Printf("%x,", Packet->Bytes.ByteBuf[i]);
}
Uart_Printf("\n");
// 协议为IP或ARP才接收
OUT_B(P0_RSAR1, Bnry); // 设远程起始地址寄存器1(高字节)
OUT_B(P0_RSAR0, 4); // 设远程起始地址寄存器0(低字节)
OUT_B(P0_RBCR1, Packet->EtherFrame.Length>>8); // 设远程字节计数寄存器1(高字节)
OUT_B(P0_RBCR0, Packet->EtherFrame.Length&0xFF); // 设远程字节计数寄存器0(高字节)
OUT_B(P_CR, 0x0A); // 设命令寄存器:选择页0, 远程读, 开始执行。
for (j=18; j<Packet->EtherFrame.Length+4; j++)
Packet->Bytes.ByteBuf[j]=IN_B(P_DMA_PORT); // 读远程DMA口
}
PrintProtocalS(Packet);
Bnry=Packet->EtherFrame.NextPage-1; // 下一页指针减1
if (Bnry<0x4C)
Bnry=0x7F;
OUT_B(P0_BNRY, Bnry); // 设接收缓冲区读页指针
return 1; // 读包成功,返回1
}
}
return 0; // 不成功,返回0
}
// ne2000 发包子程序
// 发送一个数据包的命令, 长度最小为60 字节,
// 最大1514 字节需要发送的数据包要先存放在 txdnet 缓冲区
void RTL8019AS_SndPacket0(union Frame8019 *Packet, short Length)
{
static unsigned char TransBufSwitch=0;
unsigned char i;
short j;
Page(0); // 选择页0的寄存器
if (Length<60)
Length=60;
for (i=0; i<3; i++)
Packet->EtherFrame.SourceNodeId[i]=MAC_Buf[i];
TransBufSwitch=!TransBufSwitch;
if (TransBufSwitch)
OUT_B(P0_RSAR1, 0x40); // 设远程(DMA)起始地址寄存器1=前缓冲区首页
else
OUT_B(P0_RSAR1, 0x46); // 设远程(DMA)起始地址寄存器1=后缓冲区首页
OUT_B(P0_RSAR0, 0x00); // 设远程(DMA)起始地址寄存器0(低字节)值
OUT_B(P0_RBCR1, Length>>8); // 设远程字节计数器寄存器1(高字节)
OUT_B(P0_RBCR0, Length&0xFF); // 设远程字节计数器寄存器0(低字节)
OUT_B(P_CR, 0x12); // 设命令寄存器:选择0页寄存器, 远程写DMA, 起始命令。
for (j=4; j<Length+4; j++)
OUT_B(P_DMA_PORT, Packet->Bytes.ByteBuf[j]); // 写远程DMA口
// 等待原来的数据帧发送结束
for (i=0; i<6; i++) // 最多重发6 次
{
for (j=0; j<1000; j++)
if ((IN_B(P_CR)&0x04)==0) // 读命令寄存器,检查TXP:为是否为低(传送结束或中止)
break;
if ((IN_B(P0_TSR)&0x01)!=0) // 读发送状态寄存器,检查PTX:发送成功为1时,退出;
break;
OUT_B(P_CR, 0x3E); // 设命令寄存器:选择页0,中止DMA,执行发送。
}
// 发送新的数据帧
if (TransBufSwitch)
OUT_B(P0_TPSR, 0x40); // 设发送页起始寄存器=前缓冲区首页;
else
OUT_B(P0_TPSR, 0x46); // 设发送页起始寄存器=后缓冲区首页;
OUT_B(P0_TBCR1, Length>>8); // 设发送字节计数器1(高字节)
OUT_B(P0_TBCR0, Length&0xFF); // 设发送字节计数器0(低字节)
OUT_B(P_CR, 0x3E); // 设命令寄存器:选择页0,中止DMA,执行发送。
}
// ne2000 发包子程序
// 发送一个数据包的命令, 长度最小为60 字节,
// 最大1514 字节需要发送的数据包要先存放在 txdnet 缓冲区
void RTL8019AS_SndPacket(union Frame8019 *Packet, short Length)
{
static unsigned char TransBufSwitch=0;
unsigned char i;
short j;
LookPointer(0);
Page(0); // 选择页0的寄存器
if (Length<60)
Length=60;
for (i=0; i<3; i++)
Packet->EtherFrame.SourceNodeId[i]=MAC_Buf[i];
for (i=0; i<3; i++)
Packet->EtherFrame.DestDodeId[i]=MAC_Buf[i];
Packet->EtherFrame.Protocal = 60;
for(i = 0; i < 46; i++)
{
Packet->EtherFrame.Packet[i] = i;
}
TransBufSwitch=!TransBufSwitch;
if (TransBufSwitch)
OUT_B(P0_RSAR1, 0x40); // 设远程(DMA)起始地址寄存器1=前缓冲区首页
else
OUT_B(P0_RSAR1, 0x46); // 设远程(DMA)起始地址寄存器1=后缓冲区首页
OUT_B(P0_RSAR0, 0x00); // 设远程(DMA)起始地址寄存器0(低字节)值
OUT_B(P0_RBCR1, Length>>8); // 设远程字节计数器寄存器1(高字节)
OUT_B(P0_RBCR0, Length&0xFF); // 设远程字节计数器寄存器0(低字节)
OUT_B(P_CR, 0x12); // 设命令寄存器:选择0页寄存器, 远程写DMA, 起始命令。
for (j=4; j<Length+4; j++)
OUT_B(P_DMA_PORT, Packet->Bytes.ByteBuf[j]); // 写远程DMA口
// 发送新的数据帧
if (TransBufSwitch)
OUT_B(P0_TPSR, 0x40); // 设发送页起始寄存器=前缓冲区首页;
else
OUT_B(P0_TPSR, 0x46); // 设发送页起始寄存器=后缓冲区首页;
OUT_B(P0_TBCR1, Length>>8); // 设发送字节计数器1(高字节)
OUT_B(P0_TBCR0, Length&0xFF); // 设发送字节计数器0(低字节)
OUT_B(P_CR, 0x3E); // 设命令寄存器:选择页0,中止DMA,执行发送。
}
void LookPointer(unsigned char PageNo)
{
unsigned char Bnry,Curr;
Page(0); // 选择页0的寄存器
Bnry=IN_B(P0_BNRY); // 读接收缓冲区读页指针
Page(1); // 选择第1页的寄存器
Curr=IN_B(P1_CURR); // 读接收缓冲区写页指针
Page(PageNo); // 选择第1页的寄存器
}
unsigned char aa;
void RegTest()
{
unsigned char i;
unsigned char val;
RTL8019AS_Rreset(); // 网卡复位
Uart_Printf("\nTest InitRTL8019AS() _ P_CR = %x\n", IN_B(P_CR));
OUT_B(P_CR, 0x61); //选择页0寄存器,网卡停止运行
// Page(1);
for(i = 1; i <= 15; i++)
{
OUT_B(i, i+0x80);
}
Page(0);
for(i = 1; i <= 15; i++)
{
val = 0;
val = IN_B(i);
Uart_Printf("0 val = %x i = %x\n", val, i);
/*
Page(1);
val = 0;
val = IN_B(i);
Uart_Printf("1 val = %x i = %x\n", val, i);
*/
}
Uart_Printf("aa = %x,%x\n",&aa,RegTest);
}
void RTL8019AS_Config()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -