📄 91x_enet.c
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name : 91x_enet.c
* Author : MCD Application Team
* Date First Issued : May 2006
* Description : ENET library functions
********************************************************************************
* History:
* May 2006: v1.0
* 06/19/06: v1.1
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "91x_lib.h"
#include "string.h" //include when using memcpy function
/* Include of other module interface headers ---------------------------------*/
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#ifndef NULL
#define NULL (0)
#endif
/* Function return values */
#define ENET_OK (1)
#define ENET_NOK (0)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static ENET_DMADSCRBase dmaTxDscrBase, dmaRxDscrBase;
static u8 RxBuff[1520], TxBuff[1520];
/* Private function prototypes -----------------------------------------------*/
extern MEMCOPY_L2S_BY4();
/* Interface functions -------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : ENET_MACControlConfig(ENET_MACConfig * MAC_Config)
* Description : MAC Control Register Configuration
* Input : MAC_Config structure
* Output : None
* Return : None
*******************************************************************************/
void ENET_MACControlConfig(ENET_MACConfig *MAC_Config)
{
ENET_MAC->MCR = 0;
/* ReceiveALL bit */
if (MAC_Config->ReceiveALL==ENABLE) ENET_MAC->MCR |= MAC_MCR_RA;
else ENET_MAC->MCR &=~MAC_MCR_RA;
/* MIIPrescaler */
ENET_MAC->MCR &=~(0x3<<24);
if ((MAC_Config->MIIPrescaler) == MIIPrescaler_2)
ENET_MAC->MCR |=0x1<<24;
/* Loopback mode */
if (MAC_Config->LoopbackMode==ENABLE)
{
ENET_MAC->MCR &=~MAC_MCR_LM;
ENET_MAC->MCR |=0x1<<21;
ENET_MAC->MCR &=~MAC_MCR_DRO; /*enable frame reception during transmission*/
}
/* Address filtering mode */
ENET_MAC->MCR &=~MAC_MCR_AFM;
ENET_MAC->MCR |= MAC_Config->AddressFilteringMode;
/* VLAN Filtering Mode */
ENET_MAC->MCR |= (MAC_Config->VLANFilteringMode)<<15;
/*Wrong Frame Pass */
if (MAC_Config->PassWrongFrame == ENABLE) ENET_MAC->MCR |=MAC_MCR_PWF;
else ENET_MAC->MCR &=~MAC_MCR_PWF;
/* Late Collision Retransmission*/
if (MAC_Config->LateCollision == ENABLE) ENET_MAC->MCR |=MAC_MCR_ELC;
else ENET_MAC->MCR &=~MAC_MCR_ELC;
/* Broadcast Frame Reception */
if (MAC_Config->BroadcastFrameReception == ENABLE) ENET_MAC->MCR &=~MAC_MCR_DBF;
else ENET_MAC->MCR |=MAC_MCR_DBF;
/* PacketRetry */
if (MAC_Config->PacketRetry == ENABLE) ENET_MAC->MCR &=~MAC_MCR_DPR;
else ENET_MAC->MCR |=MAC_MCR_DPR;
/* RxFrameFiltering */
if (MAC_Config->RxFrameFiltering == ENABLE) ENET_MAC->MCR |=MAC_MCR_RVFF;
else ENET_MAC->MCR &=~MAC_MCR_RVFF;
/* AutomaticPadRemoval */
if (MAC_Config->AutomaticPadRemoval == ENABLE) ENET_MAC->MCR |=MAC_MCR_APR;
else ENET_MAC->MCR &=~MAC_MCR_APR;
/* DefferalCheck */
if (MAC_Config->DeferralCheck == ENABLE) ENET_MAC->MCR |=MAC_MCR_DCE;
else ENET_MAC->MCR &=~MAC_MCR_DCE;
}
/*******************************************************************************
* Function Name : ENET_GetRxStatus
* Description : Get Rx status flags
* Input : RxStatus : pointer to structure of type ENET_RxStatus
* Output : None
* Return : None
*******************************************************************************/
void ENET_GetRxStatus(ENET_RxStatus * RxStatus)
{
u32 regvalue;
regvalue = ENET_MAC->MRS;
RxStatus->FrameAborted = (FlagStatus)((MAC_MRS_FA®value)>>31);
RxStatus->PacketFilter = (FlagStatus)((MAC_MRS_PF®value)>>30);
RxStatus->FilteringFail = (FlagStatus)((MAC_MRS_FF®value)>>29);
RxStatus->BroadCastFrame = (FlagStatus)((MAC_MRS_BF®value)>>28);
RxStatus->MulticastFrame = (FlagStatus)((MAC_MRS_MCF®value)>>27);
RxStatus->UnsupportedControFrame = (FlagStatus)((MAC_MRS_UCF®value)>>26);
RxStatus->ControlFrame = (FlagStatus)((MAC_MRS_CF®value)>>25);
RxStatus->LengthError = (FlagStatus)((MAC_MRS_LE®value)>>24);
RxStatus->Vlan2Tag = (FlagStatus)((MAC_MRS_VL2®value)>>23);
RxStatus->Vlan1Tag = (FlagStatus)((MAC_MRS_VL1®value)>>22);
RxStatus->CRCError = (FlagStatus)((MAC_MRS_CE®value)>>21);
RxStatus->ExtraBit = (FlagStatus)((MAC_MRS_EB®value)>>20);
RxStatus->MIIError = (FlagStatus)((MAC_MRS_ME®value)>>19);
RxStatus->FrameType = (FlagStatus)((MAC_MRS_FT®value)>>18);
RxStatus->LateCollision = (FlagStatus)((MAC_MRS_LC®value)>>17);
RxStatus->OverLength = (FlagStatus)((MAC_MRS_OL®value)>>16);
RxStatus->RuntFrame = (FlagStatus)((MAC_MRS_RF®value)>>15);
RxStatus->WatchDogTimout = (FlagStatus)((MAC_MRS_WT®value)>>14);
RxStatus->FalseCarrierIndication = (FlagStatus)((MAC_MRS_FCI®value)>>13);
RxStatus->FrameLength = (FlagStatus)(MAC_MRS_FL®value);
}
/*******************************************************************************
* Function Name : ENET_GetTxStatus
* Description : Get Tx status flags
* Input : TxStatus: pointer on structure of type ENET_TxStatus
* Output : None
* Return : None
*******************************************************************************/
void ENET_GetTxStatus(ENET_TxStatus * TxStatus)
{
u32 regvalue;
regvalue = ENET_MAC->MTS;
TxStatus->PacketRetry = (FlagStatus)((MAC_MTS_PR®value)>>31);
TxStatus->ByteCount = (u8)((MAC_MTS_BC®value)>>18);
TxStatus->collisionCount = (u8)((MAC_MTS_CC®value)>>10);
TxStatus->LateCollisionObserved = (FlagStatus)((MAC_MTS_LCO®value)>>9);
TxStatus->Deffered = (FlagStatus)((MAC_MTS_DEF®value)>>8);
TxStatus->UnderRun = (FlagStatus)((MAC_MTS_UR®value)>>7);
TxStatus->ExcessiveCollision = (FlagStatus)((MAC_MTS_EC®value)>>6);
TxStatus->LateCollision = (FlagStatus)((MAC_MTS_LC®value)>>5);
TxStatus->ExcessiveDefferal = (FlagStatus)((MAC_MTS_ED®value)>>4);
TxStatus->LossOfCarrier = (FlagStatus)((MAC_MTS_LOC®value)>>3);
TxStatus->NoCarrier = (FlagStatus)((MAC_MTS_NC®value)>>2);
TxStatus->FrameAborted = (FlagStatus)(MAC_MTS_FA®value);
}
/*******************************************************************************
* Function Name : ENET_SetOperatingMode
* Description : Sets the Operating mode
* Input : ENET_OperatingMode:(see ENET_OperatingMode in 91x_enet.h)
* Output : None
* Return : None
*******************************************************************************/
void ENET_SetOperatingMode(u32 ENET_OperatingMode)
{
if (ENET_OperatingMode==PHY_FULLDUPLEX_100M ||ENET_OperatingMode==PHY_FULLDUPLEX_10M )
{
ENET_MAC->MCR |=MAC_MCR_FDM; /* full duplex mode */
ENET_MAC->MCR &=~MAC_MCR_DRO; /* enable frame reception during transmission */
}
else
{
ENET_MAC->MCR &=~MAC_MCR_FDM; /* half duplex mode */
ENET_MAC->MCR |=MAC_MCR_DRO; /* disable frame reception during transmission */
}
}
/*******************************************************************************
* Function Name : ENET_MIIWriteReg
* Description : Writes a value on the PHY registers
* Input : phyDev PHY device address
: phyReg PHY register to be written
* : phyVal PHY register value
* Output : None
* Return : None
*******************************************************************************/
void ENET_MIIWriteReg (u8 phyDev, u8 phyReg, u32 phyVal)
{
u32 addr;
u32 res; /* temporary result for address register status */
u32 timeout;
/* Prepare the MII register address */
addr = 0;
addr |= ((phyDev<<11) & MAC_MII_ADDR_PHY_ADDR); /* set the PHY address */
addr |= ((phyReg<<6) & MAC_MII_ADDR_MII_REG); /* select the corresponding register */
addr |= MAC_MII_ADDR_MII_WRITE; /* in write mode */
addr |= MAC_MII_ADDR_MII_BUSY;
/* Check for the Busy flag */
timeout=0;
do
{
timeout++;
res = ENET_MAC->MIIA;
} while ((res & MAC_MII_ADDR_MII_BUSY) && (timeout < (u32 )MII_WRITE_TO));
/* Give the value to the MII data register */
ENET_MAC->MIID = (phyVal & 0xFFFF);
/* write the result value into the MII Address register */
ENET_MAC->MIIA =addr;
/* Check for the Busy flag */
timeout=0;
do
{
timeout++;
res = ENET_MAC->MIIA;
} while ((res & MAC_MII_ADDR_MII_BUSY) && (timeout < (u32 )MII_WRITE_TO));
}
/*******************************************************************************
* Function Name : ENET_MIIReadReg
* Description : Writes a value on the PHY
* Input : phyDev PHY device address
* : phyReg PHY register to be read
* Output : None
* Return : The read value (16 bits)
*******************************************************************************/
u32 ENET_MIIReadReg (u8 phyDev, u32 phyReg )
{
u32 rValue;
u32 addr;
u32 res; /* temporary result for address register status */
u32 timeout; /* timeout value for read process */
/* prepare the MII register address */
addr = 0;
addr |= ((phyDev<<11) & MAC_MII_ADDR_PHY_ADDR); /* set the PHY address */
addr |= ((phyReg<<6) & MAC_MII_ADDR_MII_REG); /* select the corresponding register */
addr &= ~(MAC_MII_ADDR_MII_WRITE); /* ... in read mode */
addr |= MAC_MII_ADDR_MII_BUSY;
/* Check for the Busy flag */
timeout = 0;
do
{
timeout++;
res = ENET_MAC->MIIA;
} while ((res & MAC_MII_ADDR_MII_BUSY) && (timeout < (u32 )MII_READ_TO));
/* write the result value into the MII Address register */
ENET_MAC->MIIA = addr;
/* Check for the Busy flag */
timeout = 0;
do
{
timeout++;
res = ENET_MAC->MIIA;
} while ((res & MAC_MII_ADDR_MII_BUSY) && (timeout < (u32 )MII_READ_TO));
/* read the result value from data register*/
rValue = ENET_MAC->MIID;
return (rValue & 0x0000FFFF);
}
/*******************************************************************************
* Function Name : ENET_RxDscrInit
* Description : Initializes the Rx ENET descriptor chain. Single Descriptor
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void ENET_RxDscrInit(void)
{
ENET_DMADSCRBase *pRXDescrChain;
/* Initialization */
pRXDescrChain = &dmaRxDscrBase;
/* Assign temp Rx array to the ENET buffer */
dmaRxDscrBase.dmaAddr = (u32)RxBuff;
/* Initialize RX ENET Status and control */
dmaRxDscrBase.dmaStatCntl = 0x0;
/* Initialize the next descriptor- In our case its single descriptor */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -