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.

Files at this revision

API Documentation at this revision

Comitter:
loopsva
Date:
Tue Sep 30 21:48:34 2014 +0000
Child:
1:cbd7e77b6e38
Commit message:
Crude driver for obtaining the K64F's Ethernet link status

Changed in this revision

k64f_EthLink.cpp Show annotated file Show diff for this revision Revisions of this file
k64f_EthLink.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/k64f_EthLink.cpp	Tue Sep 30 21:48:34 2014 +0000
@@ -0,0 +1,34 @@
+#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);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/k64f_EthLink.h	Tue Sep 30 21:48:34 2014 +0000
@@ -0,0 +1,55 @@
+#ifndef K64FLINK_H
+#define K64FLINK_H
+
+#include "mbed.h"
+
+/** Software routine used to get Ethernet Link status from the KSZ8081 
+ *  Ethernet PHY on the FRDM-K64F via the K64F MDIO interface. 
+ * 
+ * @code
+ * //See K64F_EthLink_HelloWorld example
+ * @endcode
+*/
+
+#define MDIO_MII_READY_BIT      0x00800000  //Bit in ENET_EIR reister that allows access to ENET_MMFR register
+#define MDIO_MII_LINK_BITS      0x0004      //Ethernet Link Up / Down bit in Phy's Basic Status Register
+#define MDIO_GET_LINK_REG       0x60060000  //command for ENET_MMFR register to read Phy's Basic Status
+//                                |-------  ST = 01 start of frame                  31,30
+//                                |-------  OP = 10 read                            29,28
+//                                 ||-----  PA = 00000 phy address                  27,26,25,24,23
+//                                  ||----  RA = 00001 phy register Basic Status    22,21,20,19,18
+//                                   |----  TA = 10 turn around                     17,16
+//                                    ||||  DATA
+
+
+    /** k64f_phy_status Ethernet link result enumator
+     *
+     * @param k64f_phy_status enumator
+     *
+     */
+enum k64f_phy_status {PHY_UP = 0, PHY_DOWN = 1, MDIO_TIMEOUT = 2};
+
+    /** Create k64fEthLink controller class
+     *
+     * @param k64fEthLink class
+     *
+     */
+class k64fEthLink {
+
+public:
+    /** Create a k64fEthLink object
+     *
+     * @param -none-
+     */
+    k64fEthLink();
+    /** Return Ethernet Link Status
+     *
+     * @param -none-
+     *
+     * @return the link status code from enum k64f_phy_status
+     */
+    int GetELink();
+
+};
+
+#endif
\ No newline at end of file