Ethernet link status for the K64F

Dependents:   JRO_CR2 K64F_EthLink_HelloWorld frdm_test JRO_DDSv2

Simple K64F technique to determine if the Ethernet PHY link is up or down. The routine utilizes the Ethernet MII interface to gain access to the Ethernet PHY which contains the actual link status.

I wrote this because the mbed Ethernet function does not recognize its own "int link();" function with the K64F.

k64f_EthLink.cpp

Committer:
loopsva
Date:
2014-09-30
Revision:
0:a447869e1046
Child:
1:cbd7e77b6e38

File content as of revision 0:a447869e1046:

#include "k64f_EthLink.h"
#include "rtos.h"

//--------------------------------------------------------------------------------------------------------------------------------------//
// Constructor

k64fEthLink::k64fEthLink() {
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// Get Ethernet link status on the K64F

int k64fEthLink::GetELink() {
    int mdio_timer = 0;
    uint32_t k64f_mdio_reg = ENET_EIR;
    
    //wait for MDIO interface to be ready
    do {
        k64f_mdio_reg = ENET_EIR;
        Thread::wait(1);            //release RTOS to do other functions
        mdio_timer++;
    } while(((k64f_mdio_reg & MDIO_MII_READY_BIT) == 0) && (mdio_timer < 200));
    if(mdio_timer > 198) {          //average is about 122mS
        return(MDIO_TIMEOUT);       //timeout error
    }   
    
    //get Basic Status Register
    ENET_MMFR = MDIO_GET_LINK_REG;
    wait_us(35);                    //20 is absolute minimum!!
    k64f_mdio_reg = ENET_MMFR;      //get the phy result
    if(k64f_mdio_reg & MDIO_MII_LINK_BITS) return(PHY_UP);
    return(PHY_DOWN);
}