📄 ethernet_test.c
字号:
/*
* File: ethernet_test.c
* Purpose: Test the Ethernet port on the M5275EVB
*
* Notes: Requires loopback cable.
* See "C11001.doc" for assembly instructions
*/
#include "src/init/m5275evb.h"
#include "src/init/stdlib.h"
#include "src/fat/fat.h"
#include "src/fat/nbuf.h"
#include "mii.h"
#include "ks8721.h"
/********************************************************************/
/* Buffer Descriptors -- must be aligned on a 4-byte boundary but a
* 16-byte boundary is recommended. To avoid playing games with the
* various compilers and their different extension to ANSI C, these
* buffers are aligned by allocating an extra line of data and
* adjusting the pointers in nbuf_init().
*/
uint8 unaligned_txbd[(sizeof(NBUF) * NUM_TXBDS) + 16];
uint8 unaligned_rxbd[(sizeof(NBUF) * NUM_RXBDS) + 16];
NBUF *TxNBUF;
NBUF *RxNBUF;
/* Data Buffers -- must be aligned on a 16-byte boundary. To avoid
* playing games with the various compilers and their different
* extension to ANSI C, these buffers are aligned by allocating an
* extra line of data and adjusting the pointers in nbuf_init().
*/
uint8 unaligned_txbuffer[(TX_BUFFER_SIZE * NUM_TXBDS) + 16];
uint8 unaligned_rxbuffer[(RX_BUFFER_SIZE * NUM_RXBDS) + 16];
uint8 *TxBuffer;
uint8 *RxBuffer;
/* Data to be transmitted */
const uint8 packet[] =
{
0x00, 0xCF, 0x52, 0x35, 0xC3, 0x01, 0x00, 0xCF,
0x52, 0x35, 0xC3, 0x01, 0x08, 0x00, 0x45, 0x00,
0x00, 0x3C, 0x2B, 0xE8, 0x00, 0x00, 0x20, 0x01,
0xA6, 0x1B, 0xA3, 0x0A, 0x41, 0x55, 0xA3, 0x0A,
0x41, 0x54, 0x08, 0x00, 0x0C, 0x5C, 0x01, 0x00,
0x40, 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76
};
/********************************************************************/
void
ethernet0_test(void)
{
uint32 i;
NBUF *pNbuf;
RESULTS |= ETH0_TEST;
/*
* Initialize PAR to enable Ethernet signals
*/
MCF_GPIO_PAR_FECI2C = 0x0FA0;
MCF_GPIO_PAR_FEC0HL = 0xC0;
MCF_GPIO_PAR_FEC1HL = 0xC0;
/* TEMPFIX assert reset out
MCF_RCM_RCR = MCF_RCM_RCR_FRCRSTOUT;
*/
for(i=0;i<1000;i++);
MCF_RCM_RCR = 0;
for(i=0;i<1000;i++);
TxNBUF = (NBUF *)((uint32)(unaligned_txbd + 16) & 0xFFFFFFF0);
RxNBUF = (NBUF *)((uint32)(unaligned_rxbd + 16) & 0xFFFFFFF0);
TxBuffer = (uint8 *)((uint32)(unaligned_txbuffer + 16) & 0xFFFFFFF0);
RxBuffer = (uint8 *)((uint32)(unaligned_rxbuffer + 16) & 0xFFFFFFF0);
/* Initialize receive descriptor ring */
for (i = 0; i < NUM_RXBDS; i++)
{
RxNBUF[i].status = RX_BD_E;
RxNBUF[i].length = 0;
RxNBUF[i].data = &RxBuffer[i * RX_BUFFER_SIZE];
}
/* Set the Wrap bit on the last one in the ring */
RxNBUF[NUM_RXBDS - 1].status |= RX_BD_W;
/* Initialize transmit descriptor ring */
for (i = 0; i < NUM_TXBDS; i++)
{
TxNBUF[i].status = TX_BD_L | TX_BD_TC;
TxNBUF[i].length = 0;
TxNBUF[i].data = &TxBuffer[i * TX_BUFFER_SIZE];
}
/* Set the Wrap bit on the last one in the ring */
TxNBUF[NUM_TXBDS - 1].status |= TX_BD_W;
/* Set the source address for the controller */
MCF_FEC_PALR0 = 0x00CF5235;
MCF_FEC_PAUR0 = 0xC3010000;
MCF_FEC_IALR0 = 0x00000000;
MCF_FEC_IAUR0 = 0x00000000;
MCF_FEC_GALR0 = 0x00000000;
MCF_FEC_GAUR0 = 0x00000000;
/* Set Receive Buffer Size */
MCF_FEC_EMRBR0 = (uint16)RX_BUFFER_SIZE;
/* Point to the start of the circular Rx buffer descriptor queue */
MCF_FEC_ERDSR0 = (uint32)RxNBUF;
/* Point to the start of the circular Tx buffer descriptor queue */
MCF_FEC_ETDSR0 = (uint32)TxNBUF;
MCF_FEC_RCR0 = (0
// | MCF_FEC_RCR_LOOP
| MCF_FEC_RCR_MAX_FL(1518)
| MCF_FEC_RCR_MII_MODE);
// ); /* full duplex */
/* Operate in full-duplex mode, no heart beat control */
MCF_FEC_TCR0 = 0x0004;
/* Set MII speed to be 2.5Mhz */
MCF_FEC_MSCR0 = 0x001E;
/* Grab buffer in ring */
pNbuf = TxNBUF;
/* Copy constant data into the data buffer */
memcpy(pNbuf->data, packet, 64);
/* Set the length of the packet */
pNbuf->length = 64;
/* Enable FEC */
MCF_FEC_ECR0 |= MCF_FEC_ECR_ETHER_EN;
/* Initialize the PHY */
if((ks8721_init(FEC_CH0, FEC_PHYADDR1, MII_100BASE_TX, MII_FULL_DUPLEX))==0)
{
RESULTS |= ETH0_FAIL;
return;
}
for (i = 0; i < NUM_RXBDS; i++)
{
RxNBUF[i].status = RX_BD_E;
RxNBUF[i].length = 0;
RxNBUF[i].data = &RxBuffer[i * RX_BUFFER_SIZE];
}
/* Set the Wrap bit on the last one in the ring */
RxNBUF[NUM_RXBDS - 1].status |= RX_BD_W;
/* Indicate Empty buffers have been produced */
MCF_FEC_RDAR0 = MCF_FEC_RDAR_R_DES_ACTIVE;
/* Mark packet as ready to send */
pNbuf->status |= TX_BD_R;
/* Indicate to FEC that transmit buffer is ready to send */
MCF_FEC_TDAR0 = MCF_FEC_TDAR_X_DES_ACTIVE;
for (i=0; i < 1000000; i++)
{
if (MCF_FEC_EIR0 & MCF_FEC_EIR_RXF)
{
break;
}
}
if (i == 1000000)
{
/* Timed-out */
RESULTS |= ETH0_FAIL;
return;
}
for (i = 0; i < 64; i++)
{
if (TxNBUF[0].data[i] != RxNBUF[0].data[i])
{
RESULTS |= ETH0_FAIL;
return;
}
}
/* Ethernet0 Test passed */
}
/********************************************************************/
/********************************************************************/
void
ethernet1_test(void)
{
uint32 i;
NBUF *pNbuf;
RESULTS |= ETH1_TEST;
/*
* Initialize PAR to enable Ethernet signals
*/
MCF_GPIO_PAR_FECI2C = 0x0FA0;
MCF_GPIO_PAR_FEC0HL = 0xC0;
MCF_GPIO_PAR_FEC1HL = 0xC0;
TxNBUF = (NBUF *)((uint32)(unaligned_txbd + 16) & 0xFFFFFFF0);
RxNBUF = (NBUF *)((uint32)(unaligned_rxbd + 16) & 0xFFFFFFF0);
TxBuffer = (uint8 *)((uint32)(unaligned_txbuffer + 16) & 0xFFFFFFF0);
RxBuffer = (uint8 *)((uint32)(unaligned_rxbuffer + 16) & 0xFFFFFFF0);
/* Initialize receive descriptor ring */
for (i = 0; i < NUM_RXBDS; i++)
{
RxNBUF[i].status = RX_BD_E;
RxNBUF[i].length = 0;
RxNBUF[i].data = &RxBuffer[i * RX_BUFFER_SIZE];
}
/* Set the Wrap bit on the last one in the ring */
RxNBUF[NUM_RXBDS - 1].status |= RX_BD_W;
/* Initialize transmit descriptor ring */
for (i = 0; i < NUM_TXBDS; i++)
{
TxNBUF[i].status = TX_BD_L | TX_BD_TC;
TxNBUF[i].length = 0;
TxNBUF[i].data = &TxBuffer[i * TX_BUFFER_SIZE];
}
/* Set the Wrap bit on the last one in the ring */
TxNBUF[NUM_TXBDS - 1].status |= TX_BD_W;
/* Set the source address for the controller */
MCF_FEC_PALR1 = 0x00CF5235;
MCF_FEC_PAUR1 = 0xC3010000;
MCF_FEC_IALR1 = 0x00000000;
MCF_FEC_IAUR1 = 0x00000000;
MCF_FEC_GALR1 = 0x00000000;
MCF_FEC_GAUR1 = 0x00000000;
/* Set Receive Buffer Size */
MCF_FEC_EMRBR1 = (uint16)RX_BUFFER_SIZE;
/* Point to the start of the circular Rx buffer descriptor queue */
MCF_FEC_ERDSR1 = (uint32)RxNBUF;
/* Point to the start of the circular Tx buffer descriptor queue */
MCF_FEC_ETDSR1 = (uint32)TxNBUF;
MCF_FEC_RCR1 = (0
// | MCF_FEC_RCR_LOOP
| MCF_FEC_RCR_MAX_FL(1518)
| MCF_FEC_RCR_MII_MODE);
// ); /* full duplex */
/* Operate in full-duplex mode, no heart beat control */
MCF_FEC_TCR1 = 0x0004;
/* Set MII speed to be 2.5Mhz */
MCF_FEC_MSCR1 = 0x001E;
/* Grab buffer in ring */
pNbuf = TxNBUF;
/* Copy constant data into the data buffer */
memcpy(pNbuf->data, packet, 64);
/* Set the length of the packet */
pNbuf->length = 64;
/* Enable FEC */
MCF_FEC_ECR1 |= MCF_FEC_ECR_ETHER_EN;
/* Initialize the PHY */
if((ks8721_init(FEC_CH1, FEC_PHYADDR1, MII_100BASE_TX, MII_FULL_DUPLEX))==0)
{
RESULTS |= ETH1_FAIL;
return;
}
for (i = 0; i < NUM_RXBDS; i++)
{
RxNBUF[i].status = RX_BD_E;
RxNBUF[i].length = 0;
RxNBUF[i].data = &RxBuffer[i * RX_BUFFER_SIZE];
}
/* Set the Wrap bit on the last one in the ring */
RxNBUF[NUM_RXBDS - 1].status |= RX_BD_W;
/* Indicate Empty buffers have been produced */
MCF_FEC_RDAR1 = MCF_FEC_RDAR_R_DES_ACTIVE;
/* Mark packet as ready to send */
pNbuf->status |= TX_BD_R;
/* Indicate to FEC that transmit buffer is ready to send */
MCF_FEC_TDAR1 = MCF_FEC_TDAR_X_DES_ACTIVE;
for (i=0; i < 10000000; i++)
{
if (MCF_FEC_EIR1 & MCF_FEC_EIR_RXF)
{
break;
}
}
if (i == 10000000)
{
/* Timed-out */
RESULTS |= ETH1_FAIL;
return;
}
for (i = 0; i < 64; i++)
{
if (TxNBUF[0].data[i] != RxNBUF[0].data[i])
{
RESULTS |= ETH1_FAIL;
return;
}
}
/* Ethernet1 Test passed */
}
/********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -