📄 ethernet_test.c
字号:
/*
* File: ethernet_test.c
* Purpose: Test the Ethernet port on the M5282EVB
*
* Notes: Requires loopback cable.
* See "C11001.doc" for assembly instructions
*/
#include "src/init/m5282evb.h"
#include "src/init/stdlib.h"
#include "src/fat/fat.h"
#include "src/fat/nbuf.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, 0x82, 0xC3, 0x01, 0x00, 0xCF,
0x52, 0x82, 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
ethernet_test(void)
{
uint32 i;
NBUF *pNbuf;
RESULTS |= ETH_TEST;
/* Initialize PEHLPAR to enable ethernet signals */
MCF5282_GPIO_PEHLPAR = 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;
/* Enable FEC */
//MCF5282_FEC_ECR |= MCF5282_FEC_ECR_ETHER_EN;
/* Set the source address for the controller */
MCF5282_FEC_PALR = 0x00CF5282;
MCF5282_FEC_PAUR = 0xC3010000;
MCF5282_FEC_IALR = 0x00000000;
MCF5282_FEC_IAUR = 0x00000000;
MCF5282_FEC_GALR = 0x00000000;
MCF5282_FEC_GAUR = 0x00000000;
/* Set Receive Buffer Size */
MCF5282_FEC_EMRBR = (uint16)RX_BUFFER_SIZE;
/* Point to the start of the circular Rx buffer descriptor queue */
MCF5282_FEC_ERDSR = (uint32)RxNBUF;
/* Point to the start of the circular Tx buffer descriptor queue */
MCF5282_FEC_ETDSR = (uint32)TxNBUF;
/* Set the tranceiver interface to MII mode */
MCF5282_FEC_RCR = (0
| MCF5282_FEC_RCR_MII_MODE);
/*| MCF5282_FEC_RCR_DRT); /* full duplex */
/* Operate in full-duplex mode, no heart beat control */
MCF5282_FEC_TCR = 0x0004;
/* Set MII speed to be 2.67Mhz */
MCF5282_FEC_MSCR = 0x0018;
/* 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 */
MCF5282_FEC_ECR |= MCF5282_FEC_ECR_ETHER_EN;
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 */
MCF5282_FEC_RDAR = MCF5282_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 */
MCF5282_FEC_TDAR = MCF5282_FEC_TDAR_X_DES_ACTIVE;
for (i=0; i < 100000; i++)
{
if (MCF5282_FEC_EIR & MCF5282_FEC_EIR_RXF)
{
break;
}
}
if (i == 100000)
{
/* Timed-out */
RESULTS |= ETH_FAIL;
return;
}
for (i = 0; i < 64; i++)
{
if (TxNBUF[0].data[i] != RxNBUF[0].data[i])
{
RESULTS |= ETH_FAIL;
return;
}
}
/* Ethernet Test passed */
}
/********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -