Go Down

Topic: EthernetWebServer for STM32 boards using Ethernet built-in or shields (Read 3111 times) previous topic - next topic

dannyx

I started doing this. This was for a project I had to get underway ASAP, so time was short. I got it off the ground anyway, but some stuff needs improving still, hence why I'm looking into it.

The board is connected to a GPON. If I power on both the board and GPON at the same time, the GPON initializes a lot later than the board itself, so by that point the board is past the part in setup where it tries to obtains an IP address and enters the loop without one, so it never connects to anything. I was thinking of a way to detect if there's a link state change and THEN run the Ethernet.begin() line....
DannyX

dannyx

I got it to execute the while loop for an unplugged cable by doing this:

Code: [Select]
while (Ethernet.linkStatus() != LinkON);

So I had to explicitly state that the link should be ON and ONLY ON to exit that while loop. Turns out that function can also return a third result - "unknown". Not sure WHY there's two separate states (a LinkOFF and an Unknown) and what the difference is. LinkOFF made perfect sense when the cable is unplugged entirely and what I was expecting to get in my initial code which checked for LinkOFF, but turns out I was getting "unknown" all along, so the loop was exiting....
DannyX

khoih-prog

There are too many different implementations and minute details that there is no easy way (and also not interesting job) for library writers to document and satisfy every user and all use cases. That's why the open-source community will come to help you by making the source code public. If you have some implementation issue, consult and learn from the source code, then write some short simple code to verify your assumption.

You can look at the following code snippet, taken from Ethernet_STM32 example of new Ethernet_Manager_STM32 library for a way to handle the LinkStatus


Code: [Select]


void heartBeatPrint()
{
  static int num        = 1;
  static int linkStatus = 0;
  
  localEthernetIP = Ethernet.localIP();
  
#if (USE_ETHERNET2 || USE_ETHERNET3)
  // To modify Ethernet2 library
  linkStatus = Ethernet.link();
  ET_LOGINFO3(F("localEthernetIP = "), localEthernetIP, F(", linkStatus = "), (linkStatus == 1) ? F("LinkON") : F("LinkOFF") );
  
  if ( ( linkStatus == 1 ) && ((uint32_t) localEthernetIP != 0) )
#else

  // The linkStatus() is not working with W5100. Just using IP != 0.0.0.0
  // Better to use ping for W5100
  linkStatus = (int) Ethernet.linkStatus();
  ET_LOGINFO3(F("localEthernetIP = "), localEthernetIP, F(", linkStatus = "), (linkStatus == LinkON) ? F("LinkON") : F("LinkOFF") );
  
  if ( ( (linkStatus == LinkON) || !isW5500 ) && ((uint32_t) localEthernetIP != 0) )
#endif
  {
    Serial.print(F("H"));
  }
  else
    Serial.print(F("F"));  

  if (num == 80)
  {
    Serial.println();
    num = 1;
  }
  else if (num++ % 10 == 0)
  {
    Serial.print(F(" "));
  }
}


dannyx

I thought I had it sorted out like this
Code: [Select]
while (Ethernet.linkStatus() != LinkON) {
    Serial.println("No link. Check ethernet cable...");
delay(2000);}


It does indeed work when the cable is disconnected, but I'm still missing something because now it's the opposite: I can't get OUT of the loop, even when the cable IS connected, so I ge the same message over and over every 2s...

Like I said, I'm still an absolute novice, much more so with complex stuff like this, so I'm also not sure what I should be including in my sketch...at the top of my sketch I have these two.

Code: [Select]
#include <LwIP.h>
#include <STM32Ethernet.h>


If I navigate to My Documents >Arduino>libraries I can indeed have two folders called "STM32duino_STM32Ethernet" and "STM32duino_LwIP" respectivelly, which makes sense.

If I dig deeper inside the "src" folder of the "STM32duino_STM32Ethernet" folder, I also see the "STM32Ethernet.h" file which the IDE gives in the #include, which also makes sense.
Opening this with a text editor, I also found those lines you mentioned


Code: [Select]
enum EthernetLinkStatus {
  Unknown,
  LinkON,
  LinkOFF
};

That's as far as I'm able to follow though and it all seems to make sense. I think I need to do something to the original Ethernet library found in ProgramFiles>Arduino>libraries, since it may not work with the LAN IC the Nucleo has (it's made for W5xxx). Am I on the right path at least ? I'm on a Nucleo F767ZI, since there's a lot of other possible platforms for this :o . Thought I'd give more precise details this time about what I actually have in front of my eyes, instead of just speculating. This comes at the expense of annoying people, so sorry for wall of text  :)
DannyX

Go Up