/* $Id: xemaclite_l1_polled_example.c,v 1.1 2007/05/16 07:17:10 mta Exp $
*/
/******************************************************************************
*
* XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
* AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
* SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,
* OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
* APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
* THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
* AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
* FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY
* WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
* IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
* REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
* INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE.
*
* (c) Copyright 2004-2005 Xilinx Inc.
* All rights reserved.
*
******************************************************************************/
/*****************************************************************************/
/**
* @file xemaclite_l1_example.c
*
* This file contains a example for using Emaclite hardware and driver.
* This file contains a level 1 polled example.
*
* @note
*
* The packets transmitted out should be observed on the network by a tool like
* Lanexplorer and then sent back.
*
*
*
* MODIFICATION HISTORY:
*
* Ver Who Date Changes
* ----- ---- -------- -----------------------------------------------
* 1.01a ecm 21/05/04 First release
* 1.01a sv 06/06/05 Minor changes to comply to Doxygen and coding guidelines
*
*
*****************************************************************************/
/***************************** Include Files *********************************/
#include "xparameters.h"
#include "xstatus.h"
#include "xemaclite.h"
/************************** Constant Definitions *****************************/
/*
* The following constants map to the XPAR parameters created in the
* xparameters.h file. They are defined here such that a user can easily
* change all the needed parameters in one place.
*/
#define EMAC_DEVICE_ID XPAR_ETHERNET_MAC_DEVICE_ID
#define EMACLITE_TEST_FRAME_SMALL_SIZE 46 /* Size of a samll Test Frame */
#define EMACLITE_TEST_FRAME_LARGE_SIZE 1500 /* Size of a long Test Frame */
/**************************** Type Definitions *******************************/
/***************** Macros (Inline Functions) Definitions *********************/
/************************** Function Prototypes ******************************/
XStatus EMACLitePolledExample(Xuint16 DeviceId);
static XStatus SendFrame(XEmacLite *InstancePtr, Xuint32 PayloadSize,
Xuint8 *DestAddress);
static XStatus RecvFrame(Xuint32 PayloadSize);
/************************** Variable Definitions *****************************/
/*
* Set up valid local and remote MAC addresses. The loopback tests will
* use the LocalAddress both as source and destination, while the network
* tests will use both RemoteAddress and LocalAddress
*/
static Xuint8 RemoteAddress[XEL_MAC_ADDR_SIZE] =
{
0x00, 0x10, 0xa4, 0xb6, 0xfd, 0x09
};
static Xuint8 LocalAddress[XEL_MAC_ADDR_SIZE] =
{
0x06, 0x05, 0x04, 0x03, 0x02, 0x01
};
static XEmacLite EmacLiteInstance; /* Instance of the EmacLite driver */
/*
* Buffers used for Transmission and Reception of Packets
*/
static Xuint8 TxFrame[XEL_MAX_FRAME_SIZE];
static Xuint8 RxFrame[XEL_MAX_FRAME_SIZE];
static Xuint32 RecvFrameLength;
static Xboolean TransmitComplete;
/****************************************************************************/
/**
*
* This function is the main function of the XEmaclite polled example.
*
* @param None
*
* @return XST_SUCCESS to indicate success, otherwise XST_FAILURE .
*
* @note None
*
*****************************************************************************/
int main()
{
XStatus Status;
/*
* Run the EmacLite Polled example , specify the Device ID that is
* generated in xparameters.h
*/
Status = EMACLitePolledExample(EMAC_DEVICE_ID);
if (Status != XST_SUCCESS)
{
return XST_FAILURE;
}
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* The main entry point for the EmacLite driver in polled mode example.
*
* @param DeviceId is device ID of the XEmacLite Device , typically
* XPAR__DEVICE_ID value from xparameters.h
*
* @return XST_SUCCESS to indicate success, otherwise XST_FAILURE
*
* @note None.
*
******************************************************************************/
XStatus EMACLitePolledExample(Xuint16 DeviceId)
{
XStatus Status;
XEmacLite *EmacLiteInstancePtr = &EmacLiteInstance;
Xuint32 TxLength;
RecvFrameLength = 0;
/*
* First Initialize the device.
*/
Status = XEmacLite_Initialize(EmacLiteInstancePtr, DeviceId);
if (Status != XST_SUCCESS)
{
return XST_FAILURE;
}
/*
* Set the MAC address.
*/
XEmacLite_SetMacAddress(EmacLiteInstancePtr, LocalAddress);
/*
* Empty any existing receive frames.
*/
XEmacLite_FlushReceive(EmacLiteInstancePtr);
/*
* Check if there is a tx buffer available, if there isn't it is an error.
*/
if (XEmacLite_TxBufferAvailable(EmacLiteInstancePtr) != XTRUE)
{
return XST_FAILURE;
}
/*
* Just send frames of varying sizes. Use LanExplorer to verify these
* are out on the network and then return them
*/
RecvFrameLength = 0;
for (TxLength = EMACLITE_TEST_FRAME_SMALL_SIZE;
TxLength < XEL_MAX_FRAME_SIZE; TxLength++)
{
do
{
Status = SendFrame(EmacLiteInstancePtr, TxLength, RemoteAddress);
} while (Status != XST_SUCCESS);
}
/*
* Poll the receive waiting for it to come back
*/
while ((volatile Xuint32)RecvFrameLength == 0)
{
RecvFrameLength = XEmacLite_Recv(EmacLiteInstancePtr,
(Xuint8 *)RxFrame);
}
/*
* Receive the frame sent by a Packet Generator
*/
Status = RecvFrame(EMACLITE_TEST_FRAME_SMALL_SIZE);
if ((Status != XST_SUCCESS) && (Status != XST_NO_DATA))
{
return XST_FAILURE;
}
/*
* Reset the receive frame length to 0
*/
RecvFrameLength = 0;
/*
* Poll the receive waiting for it to come back
*/
while ((volatile Xuint32)RecvFrameLength == 0)
{
RecvFrameLength = XEmacLite_Recv(EmacLiteInstancePtr,
(Xuint8 *)RxFrame);
}
/*
* Receive the frame sent by a Packet Generator
*/
Status = RecvFrame(EMACLITE_TEST_FRAME_LARGE_SIZE);
if ((Status != XST_SUCCESS) && (Status != XST_NO_DATA))
{
return XST_FAILURE;
}
return XST_SUCCESS;
}
/******************************************************************************/
/**
*
* This function sends a frame of given size.
*
* @param XEmacInstancePtr is a pointer to the XEmacLite instance to be worked
* on.
* @param PayloadSize is the size of the frame to create. The size only
* reflects the payload size, it does not include the Ethernet header
* size (14 bytes) nor the Ethernet CRC size (4 bytes).
* @param DestAddress if the address of the remote hardware the frame is to be
* sent to.
*
* @return XST_SUCCESS if successful, else a driver-specific return code
*
* @note None.
*
******************************************************************************/
static XStatus SendFrame(XEmacLite *InstancePtr, Xuint32 PayloadSize,
Xuint8 *DestAddress)
{
Xuint8 *FramePtr;
Xuint8 *AddrPtr = DestAddress;
int Index;
/*
* Set the Complete flag to false
*/
TransmitComplete = XFALSE;
/*
* Assemble the frame with a destination address and the source address
*/
FramePtr = (Xuint8 *)TxFrame;
*FramePtr++ = *AddrPtr++;
*FramePtr++ = *AddrPtr++;
*FramePtr++ = *AddrPtr++;
*FramePtr++ = *AddrPtr++;
*FramePtr++ = *AddrPtr++;
*FramePtr++ = *AddrPtr++;
/*
* Put in the source MAC address in the frame to be transmitted
*/
*FramePtr++ = LocalAddress[0];
*FramePtr++ = LocalAddress[1];
*FramePtr++ = LocalAddress[2];
*FramePtr++ = LocalAddress[3];
*FramePtr++ = LocalAddress[4];
*FramePtr++ = LocalAddress[5];
/*
* Set up the type/length field - be sure its in network order
*/
*((Xuint16 *)FramePtr) = PayloadSize;
FramePtr++;
FramePtr++;
/*
* Now fill in the data field with known values so we can verify them
* on receive.
*/
for (Index = 0; Index < PayloadSize; Index++)
{
*FramePtr++ = (Xuint8)Index;
}
/*
* Now send the frame
*/
return XEmacLite_Send(InstancePtr, (Xuint8 *)TxFrame,
PayloadSize + XEL_HEADER_SIZE);
}
/******************************************************************************/
/**
*
* This function receives a frame of given size. This function assumes polled
* mode,receives the frame and verifies its contents.
*
* @param PayloadSize is the size of the frame to receive.
* The size only reflects the payload size, it does not include the
* Ethernet header size (14 bytes) nor the Ethernet CRC size (4 bytes).
*
* @return XST_SUCCESS if successful, a driver-specific return code if not
*
* @note None.
*
******************************************************************************/
static XStatus RecvFrame(Xuint32 PayloadSize)
{
Xuint8 *FramePtr;
/*
* This assumes MAC does not strip padding or CRC
*/
if (RecvFrameLength != 0)
{
int Index;
/*
* Ignore broadcast - assume first byte indicates this
*/
if (*RxFrame == 0xFF)
{
/*
* Ignore and return success
*/
return XST_SUCCESS;
}
/*
* Verify length, which should be the payload size
*/
if ((RecvFrameLength-(XEL_HEADER_SIZE+ XEL_FCS_SIZE)) != PayloadSize)
{
return XST_LOOPBACK_ERROR;
}
/*
* Verify the frame contents
*/
FramePtr = (Xuint8 *)RxFrame;
FramePtr += XEL_HEADER_SIZE; /* Get past the header */
for (Index = 0; Index < PayloadSize; Index++)
{
if (*FramePtr++ != (Xuint8)Index)
{
return XST_LOOPBACK_ERROR;
}
}
}
return XST_SUCCESS;
}