ESP32-Gateway rev G. ETH_LAN8720 example doesn't work against Arduino ESP32 v2+

Started by tohox, March 24, 2022, 06:23:37 AM

Previous topic - Next topic

tohox

Hi,

I noticed that a sketch which worked fine with the ESP32-POE-ISO using Arduino ESP32 v2.0.2 failed when using the ESP32-Gateway rev G (using the F revision in Arduino IDE). The sketch compiled fine but the board failed to initialize ETH and reported the following:
esp.emac: emac_esp32_init(354): reset timeout
esp_eth: esp_eth_driver_install(222): init mac failed


So I tried with the ETH_LAN8720 example included in v2.0.2, v2.0.1 and v2.0.0 and ETH fails to initialize in all three cases but succeeds when using v1.0.6

I have had no such problems using the ESP32-POE-ISO board however.

Thanks,

NOTE: I believe this is the same issue mentionned in these two threads:
https://www.olimex.com/forum/index.php?topic=8521.msg32684#msg32684
https://www.olimex.com/forum/index.php?topic=8484.msg32485#msg32485

Stanimir5F

Hello tohox!

This problem is caused by the difference in hardware between the revisions of the board. From revision D and onwards the ethernet clock was moved from GPIO0 to GPIO17 and GPIO5 is added as ethernet power on/off pin. They are important elements for the initialization in the ESP32 ethernet library.

So why does it work with 1.0.6 but not with 2.0.x?
Since this package is not ours but belongs to Espressif all we can do for it is doing pull requests (PR) in their github repository which are supposed to be accepted/rejected by their admins and moderators.
In order to make the same example works for both older revisions (which some of our customers have) and the new ones some sort of flexibility should have been done.
So few years ago I made this PR  in which I added revision submenu mostly for the ESP32-Gateway board so it works with their default examples for ethernet (and also for SD card but this one is irrelevant for your question).
If you check the board the variant file for ESP32-Gateway in their package part of the file has this code:

#if ARDUINO_ESP32_GATEWAY >= 'D'
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 5
#endif
.....
#if ARDUINO_ESP32_GATEWAY >= 'F'
#define BOARD_HAS_1BIT_SDMMC
#endif

In other words if the board revisions is D or later the ethernet clock and phy power are defined, and for revisions F and later the 1bit SD MMC is defined. That's all great but how does it understand whether or not to count that part of the code as a active or be completely ignored. Well by the VALUE of the macro.
In the same PR I added this part within their board.txt:

esp32-gateway.menu.Revision.RevC=Revision C or older
esp32-gateway.menu.Revision.RevC.build.board=ESP32_GATEWAY='C'
esp32-gateway.menu.Revision.RevE=Revision E
esp32-gateway.menu.Revision.RevE.build.board=ESP32_GATEWAY='E'
esp32-gateway.menu.Revision.RevF=Revision F
esp32-gateway.menu.Revision.RevF.build.board=ESP32_GATEWAY='F'
So the macro "ESP32_GATEWAY" will have different value depending on the revision selected from the Arduino menu when board ESP32-Gateway is selected. And that was until 1.0.6. So it worked fine.

Later when they did the 2.0.0 for unknown to me reasons they changed that part with the following:

esp32-gateway.menu.Revision.RevC=Revision C or older
esp32-gateway.menu.Revision.RevC.build.board=ESP32_GATEWAY_C
esp32-gateway.menu.Revision.RevE=Revision E
esp32-gateway.menu.Revision.RevE.build.board=ESP32_GATEWAY_E
esp32-gateway.menu.Revision.RevF=Revision F
esp32-gateway.menu.Revision.RevF.build.board=ESP32_GATEWAY_F


And now when you select revision instead having the SAME macro with DIFFERENT VALUES, you have completely different macros.
So in the variant file when the "check" for the revision happens (#if ARDUINO_ESP32_GATEWAY >= 'D') the result is false because such macro doesn't even exist. And as a result the 2 defines:
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 5
are ignored and this is crucial for the initialization of the phy. That's why you get the error. And as mentioned earlier if you decide to test the SD card you will encounter similar problem because of the same reason.

I made a new this PR  but I made the mistake to basically address 2 issues within the same commit and maybe because of the other the PR wasn't accepted.

Anyway - the way you can workaround is by basically add at the start of the sketch these 2 macros:
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 5

Try this and see if it will solve your problem.

Stan, Olimex
May the Source be with You!

tohox

Hi Stan,

Thanks for this very comprehensive reply!

I recall I did try changing the ETH_CLK_MODE and ETH_PHY_POWER defines but it didn't work back then. Perhaps I didn't use the appropriate pin numbers.

Since then however I was able to use the ESP32-POE-ISO board definition to program the ESP32-GATEWAY and it appears to be working normally.

Again, many thanks for the thorough explanation!

enrique

Hi!
I wanted to report that my GATEWAY v.G board (purchased in June 2022) programmed with Arduino and ESP32 2.0.3 also failed to setup the Ethernet. But as reported here, adding the following two lines at the beggining of the Arduino code made it work.

#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 5

Thanks for your excellent support and products!