?? ip51_mac.c
字號:
#include "C:\wql\tcpipsocket\target.h"
#include "C:\wql\tcpipsocket\IP51_MAC.h"
#include "C:\wql\tcpipsocket\IP51_sfr.h"
#include "C:\wql\tcpipsocket\err.h"
//#include "reg52.h"
#include <stdio.h>
static UINT8 far mrx_buf[MAC_RBUFFER_SIZE];
static UINT8 far mtx_cache[MAC_TXCACHE_SIZE];
static UINT8 far mtx_buf[MAC_TBUFFER_SIZE];
static UINT8 PAUSE_ON = 0;
static ptr_2_c idata mrbuf_header;
static ptr_2_c idata mrbuf_tailer;
static ptr_2_c idata mtbuf_header;
static ptr_2_c idata mtbuf_tailer;
static ptr_2_c idata mtcache_header;
static ptr_2_c idata rin_ptr;
static ptr_2_c idata rout_ptr;
static ptr_2_c idata tin_ptr;
static ptr_2_c idata tout_ptr;
static UINT16 idata rxbuf_lft=MAC_RBUFFER_SIZE;
static UINT16 idata txbuf_lft=MAC_TBUFFER_SIZE;
UINT8 idata rx_pkt_nums=0;
static UINT8 idata tx_pkt_nums=0;
static restart = 0;
/*the pkt_num indicate the number of packets
in the mac recieve buffer
ATTENTION:there should no more than 255
packet in the buffer
*/
static ptr_2_c idata rin_stk[3];
static ptr_2_c idata rout_stk[3];
void
Init_mac(void)
{
UINT8 couten_bk;
/*********************************************/
/**********Initial the global var*************/
/*********************************************/
rin_ptr.ptr = &mrx_buf[0];
rout_ptr.ptr = &mrx_buf[0];
tin_ptr.ptr = &mtx_buf[0];
tout_ptr.ptr = &mtx_buf[0];
mrbuf_header.ptr = &mrx_buf[0];
mrbuf_tailer.ptr = &mrx_buf[MAC_RBUFFER_SIZE-1];
mtbuf_header.ptr = &mtx_buf[0];
mtbuf_tailer.ptr = &mtx_buf[MAC_TBUFFER_SIZE-1];
mtcache_header.ptr= &mtx_cache[0];
/*********************************************/
/**********Initial the MAC Core***************/
/*********************************************/
//soft reset the MAC core
MADDR = 0x00; //select IP51-MAC's MAC1 reg
MDATH = 0x80; //reset MAC
MDATL = 0x00;
MDATH = 0x00;//remove MAC rst
#ifdef FULL_DX
MDATL |= 0x0c;
#endif//FULL_DX
MADDR = 0x01; //select IP51-MAC's MAC2 reg
MDATH = 0x02;
MDATL = 0xB2; //auto PAD,Length check,Half-duplex,append CRC
#ifdef FULL_DX
MDATL |= 0x01;
#endif//FULL_DX
MADDR = 0x02;
#ifdef FULL_DX
MDATL = 0x15;
#else
MDATL = 0x12;
#endif
MADDR = 0x03;
MDATH = 0x0c;
MDATL = 0x12;
MADDR = 0x05;
MDATH = ETH_MTUH; //set MTU
MDATL = ETH_MTUL;
//set Ethernet MAC address
MADDR = 0x10;
MDATH = SA0;
MDATL = SA1;
MADDR = 0x11;
MDATH = SA2;
MDATL = SA3;
MADDR = 0x12;
MDATH = SA4;
MDATL = SA5;
MADDR = 0x13;
#ifdef ETH_PRO//receive any pkt
MDATL |= 0x08;
#endif//ETH_PRO
#ifdef ETH_PRM//receive any multicast pkt
MDATL |= 0x40;
#endif//ETH_PRM
#ifdef ETH_AMC//receive qualified multicast pkt
MDATL |= 0x20;
#endif//ETH_AMC
#ifdef ETH_ABC
MDATL |= 0x01;//accept broadcast pkts
#endif//ETH_ABC
CTR |= 0x01;//MAC CORE auto insert source MAC addr
/*********************************************/
/*********Initial MAC DMA*********************/
/*********************************************/
couten_bk = COUTEN;
COUTEN &= 0xcf;
MRPG = mrbuf_header.p.type;//when mrx_buf is not in page 1,shoule pay attention
MRBAL = mrbuf_header.p.low;
MRBAH = mrbuf_header.p.high;
MRBALE = mrbuf_tailer.p.low;
MRBAHE = mrbuf_tailer.p.high;
MRUTHE = 0x08;//10--64byte;08--32byte
MTPG = mtcache_header.p.type;//when mtx_buf is not in page 1,shoule pay attention
COUTEN = couten_bk;
INTMSK |= 0xff;//enable MAC's interrupt
couten_bk = INTST;//clear interrupt register
EXIF = 0x00;//clear MAC interupt flag
EIE |= 0x08;//Enable MAC Interrupt
MADDR = 0x00;
MDATL = 0x01; //enable Reveive
MRPG |= 0x80; //start MAC RX DMA
return;
}
UINT8
Rd_pkt_num(void)
{
return rx_pkt_nums;
}
UINT16
Rd_pkt_len(void)
{
return ( ((mdma_hdr *)rout_ptr.ptr)->len.i );
}
err_t
Rd_pkt(UINT8 *desptr)
{
if(rx_pkt_nums==0)
return ERR_MRXEMP;
else
{
ptr_2_c tmp_ptr;
UINT8 couten_bk;
UINT8 status;
EA = 0;
// rout_stk[2] = rout_stk[1];
// rout_stk[1] = rout_stk[0];
// rout_stk[0] = rout_ptr;
couten_bk = COUTEN;
COUTEN |= 0x30;//select BOOT DMA
status = ((mdma_hdr *)rout_ptr.ptr)->sta;
switch(status)
{
case 0xaa:
//this is not a valid pkt due to buffer overflow,
//which a at the end of buffer
rxbuf_lft += mrbuf_tailer.ptr -rout_ptr.ptr;
rout_ptr = mrbuf_header;
case 0x00://received a correct pkt
case 0x01://received a correct pkt,but not in continuous buffer
case 0x02:
case 0x03:
case 0x80:
case 0x81:
case 0x82:
case 0x83:
BTSPAG = rout_ptr.p.type;//when mrx_buf is not in page 1,shoule pay attention
BTSCPL = rout_ptr.p.low;
BTSCPH = rout_ptr.p.high;
BTSHDL = mrbuf_header.p.low;
BTSHDH = mrbuf_header.p.high;
BTSEDL = mrbuf_tailer.p.low;
BTSEDH = mrbuf_tailer.p.high;
tmp_ptr.ptr = desptr;
BTTPAG = tmp_ptr.p.type;//when mrx_buf is not in page 1,shoule pay attention
BTTCPL = tmp_ptr.p.low;
BTTCPH = tmp_ptr.p.high;
BTTEDL = 0xFF;//Attention:user should make sure the read buffer
BTTEDH = 0xFF;//is large enough,otherwise there will be data overlay
BTLTHL = ((mdma_hdr *)rout_ptr.ptr)->len.u8.u8_2;
BTLTHH = ((mdma_hdr *)rout_ptr.ptr)->len.u8.u8_1;
BTCTRL |= 0x80;//start MEM copy
BTTPAG = BTSPAG;
BTTCPL = BTSCPL;
BTTCPH = BTSCPH;
BTLTHH = 0x00;
BTLTHL = 0x04; //copy the MAC's CRC--4 byte
BTCTRL |= 0x80;
rx_pkt_nums --;
{
rxbuf_lft += ((mdma_hdr *)rout_ptr.ptr)->len.i + 4;
if(((mdma_hdr *)rout_ptr.ptr)->len.i > 0x600)
while(1);
/* rlft_b[4] = rlft_b[3];
rlft_b[3] = rlft_b[2];
rlft_b[2] = rlft_b[1];
rlft_b[1] = rlft_b[0];
rlft_b[0] = rxbuf_lft;*/
/* if( rxbuf_lft> MAC_RBUFFER_SIZE)
{
tmp_ptr.p.low = ((mdma_hdr *)rout_ptr.ptr)->len.u8.u8_2;//for debug
tmp_ptr.p.low = ((mdma_hdr *)rout_ptr.ptr)->len.u8.u8_1;//for debug
tmp_ptr.p.type = ((mdma_hdr *)rout_ptr.ptr)->sta;//for debug;
while(1);
}*/
rout_ptr.p.low = BTSCPL;
rout_ptr.p.high = BTSCPH;
}
break;
default ://should no other value
tmp_ptr.p.low = ((mdma_hdr *)rout_ptr.ptr)->len.u8.u8_2;//for debug
tmp_ptr.p.low = ((mdma_hdr *)rout_ptr.ptr)->len.u8.u8_1;//for debug
tmp_ptr.p.type = ((mdma_hdr *)rout_ptr.ptr)->sta;//for debug;
while(1);
break;
}
if(PAUSE_ON && (rxbuf_lft >MAC_RXBUF_THRE))
{
#ifdef FULL_DX
Rx_pause(0);
#else
Rx_bkp(0);
#endif
PAUSE_ON = 0;
}
EA = 1;
COUTEN &= 0xCF;//select MAC DMA
if(((MRPG & 0x80) == 0) && (rxbuf_lft > ETH_MTU))
MRPG |= 0x80;//enable MAC RXDMA
COUTEN = couten_bk;
if(status & 0x82)
return ERR_MRPKT_INVALID;
return ERR_OK;
}
}
#ifdef FULL_DX
void Rx_pause(UINT8 sw)
{
if(sw == 0)
{
MFCTL |= 0x80;
printf("pau 0\n");
}
else
{
MFCTL |= 0X40;
printf("pau 1\n");
}
}
#else
void Rx_bkp(UINT8 bkp_t)
{
if(bkp_t != 0)
MFCTL = 0x20 | (bkp_t & 0x0f);
else
MFCTL = 0;
return;
}
#endif//FULL_DX
void Tx_pkt(void)
{
UINT8 couten_bk;
int_2_c len;
EA = 0;
if(tx_pkt_nums == 0)
return;
couten_bk = COUTEN;
COUTEN |= 0x30;//select BOOT DMA
//copy the pkt data to the send cache
BTSPAG = tout_ptr.p.type;//Attention
BTTPAG = mtcache_header.p.type;//
BTSCPL = tout_ptr.p.low;
BTSCPH = tout_ptr.p.high;
BTSHDL = mtbuf_header.p.low;
BTSHDH = mtbuf_header.p.high;
BTSEDL = mtbuf_tailer.p.low;
BTSEDH = mtbuf_tailer.p.high;
BTTCPL = mtcache_header.p.low;
BTTCPH = mtcache_header.p.high;
BTTEDL = 0xFF;//Attention Make sure tx cache is enough
BTTEDH = 0xFF;//otherwise data will be overlay
BTLTHL = 2;
BTLTHH = 0;
BTCTRL |= 0x80;//copy len data to mtx_cache
len.i = *((UINT16 *)mtcache_header.ptr);
// tout_ptr.p.low = BTSCPL;//update tout_ptr
// tout_ptr.p.high= BTSCPH;//update tout_ptr
BTLTHL = len.u8.u8_2;
BTLTHH = len.u8.u8_1;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -