ethernet.link()

16 Nov 2009

I am developing a basic UI for my mbed attached to an ethernet cable. I want it to be able to detect if there is a connection and display "Connected" while if there is no connection to display "Not Connected".

I have looked at the ethernet library and found link();

It states that if there is a link this variable will pass a 1 and a 0 if there is no link.

I am also using the HTTP lbrary to request a search from google after it is connected. It seems as though link() does not send anything but a zero even if it is connected.

     
 if (ether.link() == 1) {
        http.get(url, result, 2000);
 }

    else {
        lcd.cls();
        lcd.printf("No Connection!");
    }

I noticed that if I call the link() after I use http.get(url, result, 2000); the result is that link() works except if the ethernet is not connected it does not even get past http.get which never makes it to the connected test.

What am I doing wrong and how can implement link() to provide me with a correct value.

17 Nov 2009

I have managed to make it work with a flipper using a timeout. It seems like link(); cannot be used to check connection settings at startup since the mbed will freeze with any http.get command that cannot be processed. Am I correct?

17 Nov 2009

At the moment I'm not able to test it for you, but I came up with to things.

1. Is the ether.link() executed as first thing or is there some prior code? It might be that for some reason the ethernet PHY needs to start up properly before it will be able to communicate.

2. Following on the first item: it could be possible that http.get() initializes the interface. I'lll have to take a closer look, but I would say that just checking for a link doesn't go without any init.

17 Nov 2009

 

Gert-Jan Admiraal wrote:

1. Is the ether.link() executed as first thing or is there some prior code? It might be that for some reason the ethernet PHY needs to start up properly before it will be able to communicate.

Yes your right, the link() function just reads out a register from the PHY. Therefore it is needed to initialize the Ethernet before. But if you have an ether object you already done that by calling the constructor. And it should work.

Gert-Jan Admiraal wrote:

2. Following on the first item: it could be possible that http.get() initializes the interface.

This is true as well. If you have a closer look here (Line 5 & 112 and many more) you will see that the lwIP stack uses this object as its backend.

From what I've learned about the lwIP stack is that it does not rely on the PHY link flag to find out whether an interface is up or not. Maybe I’ve picked the wrong bit for the new PHY, I will investigate that.

But anyway I would not recommend using multiple Ethernet objects. Not because it will not work, but it will initialize the interface multiple times and this can result in packet lost on the Ethernet.

Furthermore lwIP is written in a way that it has to own the interface. At the moment there is no way to ask lwIP if it is up or down. But in the current implementation of the HTTPClient it will initialize the TCP/IP stack the first time it gets used.

There is no multitasking on the mbed target. Therefore the initialization of the TCP/IP stack will stall the program until it's initialized.

 

Vlad if I understand that right you try to find out whether you have a cable connected or not. And drive your program in online or offline mode. That sounds like exactly what I had the link function in mind, great idea!!! I will have a look at the link function.

But be careful with your second Ethernet object ;-)

 

Rolf

 

17 Nov 2009

Thanks for your reply guys,

 

The only real issue with the link() function is the fact that http.get() never times out and lets my if statment display not connected. I have created a timeout function that kicks out the code after 10 second if it has not passed the http.get()

This timeout should really be created in the http class to let code still process if there is no connection.

17 Nov 2009

Hi guys,

If was playing with the link function a while. After I've became completely depressed, caused by the fact that the link function does give me just zeros regardless if it is plugged in or not and the code of the link function is something simple like "return PHY_REG_STS & PHY_STS_LINK" and everything seems to work but not the hardware link detection, I was simply pushing the reset button of my mbed and looking at my router. I've mentioned that the status led of the port came up ca. a second after the mbed was back. So I've simply tried:

#include "mbed.h"

DigitalOut led(LED1);
Ethernet eth;

int main() {
    wait(5);
    if(eth.link()) {
        printf("online\n");
    } else {
        printf("offline\n");
    }
    while(1) {
        led = !led;
        wait(0.2);
    }
}

And see, it works. I've read a little bit about it. The link bit is a result of a kind of send receive pulse test on the wire. This is done all 16ms. It just seems that propagating the result is a little bit slower. I guess waiting a second is hardly enough.

Cheers

Rolf

17 Nov 2009 . Edited: 17 Nov 2009

Is it the wait(5); that makes this work. Was I checking much to quickly for the mbed to realize it was online?

17 Nov 2009

Vlad, yes indeed ;-D

Such a simple thing, but it was hard to track down.

And defiantly worthy do be documented.

07 Jun 2012

currently the code that I am using keeps printing out the following string statement. Is there a way that I can control it to print it out just once when it detects the cable at any time after the programme is started.

while (1) { if (connect.link()) { pc.printf("detects the ethernet cable"); }

}