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.

Revision:
4:9d2a4dc03170
Parent:
3:fbd2336e1193
--- a/k64f_EthLink.cpp	Fri Nov 07 22:05:00 2014 +0000
+++ b/k64f_EthLink.cpp	Thu May 19 17:20:55 2016 +0000
@@ -9,7 +9,7 @@
 
 //--------------------------------------------------------------------------------------------------------------------------------------//
 // Get Ethernet link status on the K64F
-
+/*
 int k64fEthLink::GetELink() {
     int mdio_timer = 0;
     uint32_t k64f_mdio_reg = ENET_EIR;
@@ -35,6 +35,69 @@
     if(k64f_mdio_reg & MDIO_MII_LINK_BITS) return(PHY_UP);
     return(PHY_DOWN);
 }
+*/
+//--------------------------------------------------------------------------------------------------------------------------------------//
+// Get Ethernet link status on the K64F
+/*
+ENET_Type miiSTR = {};
+
+int k64fEthLink::GetELink() {
+    int mdio_timer = 0;
+    uint32_t k64f_mdio_reg = miiSTR.EIR;
+    
+    //wait for MDIO interface to be ready
+    do {
+        k64f_mdio_reg = miiSTR.EIR;
+#ifdef RTOS_H
+        Thread::wait(1);
+#else   
+        wait_ms(1);
+#endif
+        mdio_timer++;
+    } while(((k64f_mdio_reg & MDIO_MII_READY_BIT) == 0) && (mdio_timer < 300));
+    if(mdio_timer > 298) {          //average is about 122mS
+        return(MDIO_TIMEOUT);       //timeout error
+    }   
+    
+    //get Basic Status Register
+    miiSTR.MMFR = MDIO_GET_LINK_REG;
+    wait_us(35);                    //20 is absolute minimum!!
+    k64f_mdio_reg = miiSTR.MMFR;      //get the phy result
+    if(k64f_mdio_reg & MDIO_MII_LINK_BITS) return(PHY_UP);
+    return(PHY_DOWN);
+}
+*/
+//--------------------------------------------------------------------------------------------------------------------------------------//
+// Get Ethernet link status on the K64F
+
+//see: mbed-dev/targets/cmsis/TARGET_K64F/MK64F12.h
+
+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;
+#ifdef RTOS_H
+        Thread::wait(1);
+#else   
+        wait_ms(1);
+#endif
+        mdio_timer++;
+    } while(((k64f_mdio_reg & MDIO_MII_READY_BIT) == 0) && (mdio_timer < 300));
+    if(mdio_timer > 298) {          //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);
+}
+
 #endif