⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xemacps_control.c

📁 自学ZedBoard:使用IP通过ARM PS访问FPGA(源代码)
💻 C
📖 第 1 页 / 共 3 页
字号:
/* $Id: xemacps_control.c,v 1.1.2.1 2011/01/20 03:39:02 sadanan Exp $ *//******************************************************************************** (c) Copyright 2009-2010 Xilinx, Inc. All rights reserved.** This file contains confidential and proprietary information of Xilinx, Inc.* and is protected under U.S. and international copyright and other* intellectual property laws.** DISCLAIMER* This disclaimer is not a license and does not grant any rights to the* materials distributed herewith. Except as otherwise provided in a valid* license issued to you by Xilinx, and to the maximum extent permitted by* applicable law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND WITH ALL* FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS,* IMPLIED, OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF* MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE;* and (2) Xilinx shall not be liable (whether in contract or tort, including* negligence, or under any other theory of liability) for any loss or damage* of any kind or nature related to, arising under or in connection with these* materials, including for any direct, or any indirect, special, incidental,* or consequential loss or damage (including loss of data, profits, goodwill,* or any type of loss or damage suffered as a result of any action brought by* a third party) even if such damage or loss was reasonably foreseeable or* Xilinx had been advised of the possibility of the same.** CRITICAL APPLICATIONS* Xilinx products are not designed or intended to be fail-safe, or for use in* any application requiring fail-safe performance, such as life-support or* safety devices or systems, Class III medical devices, nuclear facilities,* applications related to the deployment of airbags, or any other applications* that could lead to death, personal injury, or severe property or* environmental damage (individually and collectively, "Critical* Applications"). Customer assumes the sole risk and liability of any use of* Xilinx products in Critical Applications, subject only to applicable laws* and regulations governing limitations on product liability.** THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS PART OF THIS FILE* AT ALL TIMES.*******************************************************************************//*****************************************************************************//** * * @file xemacps_control.c * * Functions in this file implement general purpose command and control related * functionality. See xemacps.h for a detailed description of the driver. * * <pre> * MODIFICATION HISTORY: * * Ver   Who  Date     Changes * ----- ---- -------- ------------------------------------------------------- * 1.00a wsy  01/10/10 First release * </pre> *****************************************************************************//***************************** Include Files *********************************/#include "xemacps.h"/************************** Constant Definitions *****************************//**************************** Type Definitions *******************************//***************** Macros (Inline Functions) Definitions *********************//************************** Function Prototypes ******************************//************************** Variable Definitions *****************************//*****************************************************************************//** * Set the MAC address for this driver/device.  The address is a 48-bit value. * The device must be stopped before calling this function. * * @param InstancePtr is a pointer to the instance to be worked on. * @param AddressPtr is a pointer to a 6-byte MAC address. * @param Index is a index to which MAC (1-4) address. * * @return * - XST_SUCCESS if the MAC address was set successfully * - XST_DEVICE_IS_STARTED if the device has not yet been stopped * *****************************************************************************/int XEmacPs_SetMacAddress(XEmacPs *InstancePtr, void *AddressPtr, u8 Index){	u32 MacAddr;	u8 *Aptr = (u8 *) AddressPtr;	Xil_AssertNonvoid(InstancePtr != NULL);	Xil_AssertNonvoid(AddressPtr != NULL);	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);	Xil_AssertNonvoid((Index <= XEMACPS_MAX_MAC_ADDR) && (Index > 0));	/* Be sure device has been stopped */	if (InstancePtr->IsStarted == XIL_COMPONENT_IS_STARTED) {		return (XST_DEVICE_IS_STARTED);	}        /* Index ranges 1 to 4, for offset calculation is 0 to 3. */        Index--;	/* Set the MAC bits [31:0] in BOT */	MacAddr = Aptr[0];	MacAddr |= Aptr[1] << 8;	MacAddr |= Aptr[2] << 16;	MacAddr |= Aptr[3] << 24;	XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,			   (XEMACPS_LADDR1L_OFFSET + Index * 8), MacAddr);	/* There are reserved bits in TOP so don't affect them */	MacAddr = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,				    (XEMACPS_LADDR1H_OFFSET + (Index * 8)));	MacAddr &= ~XEMACPS_LADDR_MACH_MASK;	/* Set MAC bits [47:32] in TOP */	MacAddr |= Aptr[4];	MacAddr |= Aptr[5] << 8;	XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,			   (XEMACPS_LADDR1H_OFFSET + (Index * 8)), MacAddr);	return (XST_SUCCESS);}/*****************************************************************************//** * Get the MAC address for this driver/device. * * @param InstancePtr is a pointer to the instance to be worked on. * @param AddressPtr is an output parameter, and is a pointer to a buffer into *        which the current MAC address will be copied. * @param Index is a index to which MAC (1-4) address. * *****************************************************************************/void XEmacPs_GetMacAddress(XEmacPs *InstancePtr, void *AddressPtr, u8 Index){	u32 MacAddr;	u8 *Aptr = (u8 *) AddressPtr;	Xil_AssertVoid(InstancePtr != NULL);	Xil_AssertVoid(AddressPtr != NULL);	Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);	Xil_AssertVoid((Index <= XEMACPS_MAX_MAC_ADDR) && (Index > 0));        /* Index ranges 1 to 4, for offset calculation is 0 to 3. */        Index--;	MacAddr = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,				    (XEMACPS_LADDR1L_OFFSET + (Index * 8)));	Aptr[0] = (u8) MacAddr;	Aptr[1] = (u8) (MacAddr >> 8);	Aptr[2] = (u8) (MacAddr >> 16);	Aptr[3] = (u8) (MacAddr >> 24);	/* Read MAC bits [47:32] in TOP */	MacAddr = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,				    (XEMACPS_LADDR1H_OFFSET + (Index * 8)));	Aptr[4] = (u8) MacAddr;	Aptr[5] = (u8) (MacAddr >> 8);}/*****************************************************************************//** * Set 48-bit MAC addresses in hash table. * The device must be stopped before calling this function. * * The hash address register is 64 bits long and takes up two locations in * the memory map. The least significant bits are stored in hash register * bottom and the most significant bits in hash register top. * * The unicast hash enable and the multicast hash enable bits in the network * configuration register enable the reception of hash matched frames. The * destination address is reduced to a 6 bit index into the 64 bit hash * register using the following hash function. The hash function is an XOR * of every sixth bit of the destination address. * * <pre> * hash_index[05] = da[05]^da[11]^da[17]^da[23]^da[29]^da[35]^da[41]^da[47] * hash_index[04] = da[04]^da[10]^da[16]^da[22]^da[28]^da[34]^da[40]^da[46] * hash_index[03] = da[03]^da[09]^da[15]^da[21]^da[27]^da[33]^da[39]^da[45] * hash_index[02] = da[02]^da[08]^da[14]^da[20]^da[26]^da[32]^da[38]^da[44] * hash_index[01] = da[01]^da[07]^da[13]^da[19]^da[25]^da[31]^da[37]^da[43] * hash_index[00] = da[00]^da[06]^da[12]^da[18]^da[24]^da[30]^da[36]^da[42] * </pre> * * da[0] represents the least significant bit of the first byte received, * that is, the multicast/unicast indicator, and da[47] represents the most * significant bit of the last byte received. * * If the hash index points to a bit that is set in the hash register then * the frame will be matched according to whether the frame is multicast * or unicast. * * A multicast match will be signaled if the multicast hash enable bit is * set, da[0] is logic 1 and the hash index points to a bit set in the hash * register. * * A unicast match will be signaled if the unicast hash enable bit is set, * da[0] is logic 0 and the hash index points to a bit set in the hash * register. * * To receive all multicast frames, the hash register should be set with * all ones and the multicast hash enable bit should be set in the network * configuration register. * * * @param InstancePtr is a pointer to the instance to be worked on. * @param AddressPtr is a pointer to a 6-byte MAC address. * * @return * - XST_SUCCESS if the HASH MAC address was set successfully * - XST_DEVICE_IS_STARTED if the device has not yet been stopped * - XST_INVALID_PARAM if the HASH MAC address passed in does not meet *   requirement after calculation * * @note * Having Aptr be unsigned type prevents the following operations from sign * extending. *****************************************************************************/int XEmacPs_SetHash(XEmacPs *InstancePtr, void *AddressPtr){	u32 HashAddr;	u8 *Aptr = (u8 *) AddressPtr;        u8 Temp1, Temp2, Temp3, Temp4, Temp5, Temp6, Temp7, Temp8;        int Result;	Xil_AssertNonvoid(InstancePtr != NULL);	Xil_AssertNonvoid(AddressPtr != NULL);	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);	/* Be sure device has been stopped */	if (InstancePtr->IsStarted == XIL_COMPONENT_IS_STARTED) {		return (XST_DEVICE_IS_STARTED);	}        Temp1 =   Aptr[0] & 0x3F;        Temp2 = ((Aptr[0] >> 6) & 0x3) | ((Aptr[1] & 0xF) << 2);        Temp3 = ((Aptr[1] >> 4) & 0xF) | ((Aptr[2] & 0x3) << 4);        Temp4 = ((Aptr[2] >> 2) & 0x3F);        Temp5 =   Aptr[3] & 0x3F;        Temp6 = ((Aptr[3] >> 6) & 0x3) | ((Aptr[4] & 0xF) << 2);        Temp7 = ((Aptr[4] >> 4) & 0xF) | ((Aptr[5] & 0x3) << 4);        Temp8 = ((Aptr[5] >> 2) & 0x3F);        Result = Temp1 ^ Temp2 ^ Temp3 ^ Temp4 ^ Temp5 ^ Temp6 ^ Temp7 ^ Temp8;        if (Result >= XEMACPS_MAX_HASH_BITS) {	        return (XST_INVALID_PARAM);        }        if (Result < 32) {	        HashAddr = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,				    XEMACPS_HASHL_OFFSET);                HashAddr |= (1 << Result);	        XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,                                    XEMACPS_HASHL_OFFSET, HashAddr);        } else {	        HashAddr = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,				    XEMACPS_HASHH_OFFSET);                HashAddr |= (1 << (Result - 32));	        XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,                                    XEMACPS_HASHH_OFFSET, HashAddr);        }	return (XST_SUCCESS);}/*****************************************************************************//** * Clear the Hash registers for the mac address pointed by AddressPtr. * * @param InstancePtr is a pointer to the instance to be worked on. * *****************************************************************************/void XEmacPs_ClearHash(XEmacPs *InstancePtr){	Xil_AssertVoid(InstancePtr != NULL);	Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);	XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,				    XEMACPS_HASHL_OFFSET, 0x0);	/* write bits [63:32] in TOP */	XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,				    XEMACPS_HASHH_OFFSET, 0x0);}/*****************************************************************************//** * Get the Hash address for this driver/device. * * @param InstancePtr is a pointer to the instance to be worked on. * @param AddressPtr is an output parameter, and is a pointer to a buffer into *        which the current HASH MAC address will be copied. * *****************************************************************************/void XEmacPs_GetHash(XEmacPs *InstancePtr, void *AddressPtr){	u32 *Aptr = (u32 *) AddressPtr;	Xil_AssertVoid(InstancePtr != NULL);	Xil_AssertVoid(AddressPtr != NULL);	Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);	Aptr[0] = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,				    XEMACPS_HASHL_OFFSET);	/* Read Hash bits [63:32] in TOP */	Aptr[1] = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,				    XEMACPS_HASHH_OFFSET);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -