November 07, 2024, 04:37:04 PM

ESP32 POE2

Started by leonardo.zambonelli, August 19, 2024, 01:44:58 PM

Previous topic - Next topic

leonardo.zambonelli

Hi all,
i'm trying to use the board ESP32 POE2, using native ESP-IDF (ver 4.X).
I'have some problem with the clock connected to GPIO0. When i start Network driver, it stops logging to console.
I've tried to change clock pin to 17 but not working.

Any ideas?
Thanks in advance

LubOlimex

Identify what is the ESP32 module on your ESP32-POE2. It is probably WROVER. This means Ethernet clock is pin #0. But there is more than that, there is PHY enable pin GPIO12. The definitions should look like that:

#ifndef ETH_PHY_TYPE
#define ETH_PHY_TYPE ETH_PHY_LAN8720
#define ETH_PHY_ADDR 0
#define ETH_PHY_MDC 23
#define ETH_PHY_MDIO 18
#define ETH_PHY_POWER 12
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_OUT
#endif
Technical support and documentation manager at Olimex

leonardo.zambonelli

This is my code:
#include "esp_eth.h"
#include "esp_event.h"
#include "esp_log.h"

#include "driver/gpio.h"

static const char *TAG = "eth_example";

// Callback per gestire gli eventi Ethernet
static void eth_event_handler(void *arg,
    esp_event_base_t event_base,
    int32_t event_id,
    void *event_data) {
    switch (event_id) {
    case ETHERNET_EVENT_CONNECTED:
        ESP_LOGI(TAG, "Ethernet Connected");
        break;
    case ETHERNET_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "Ethernet Disconnected");
        break;
    case ETHERNET_EVENT_START:
        ESP_LOGI(TAG, "Ethernet Started");
        break;
    case ETHERNET_EVENT_STOP:
        ESP_LOGI(TAG, "Ethernet Stopped");
        break;
    default:
        ESP_LOGI(TAG, "eth_event_handler. EventID: %d ", event_id);
        break;
    }
}
#define PHY_POWER_PIN 12  // Sostituisci con il pin corretto

void app_main(void) {
    // Inizializzazione del loop degli eventi
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    // Configurazione del pin di alimentazione del PHY
    gpio_pad_select_gpio(PHY_POWER_PIN);
    gpio_set_direction(PHY_POWER_PIN, GPIO_MODE_OUTPUT);
    gpio_set_level(PHY_POWER_PIN, 1); // Imposta il livello logico alto per accendere il PHY
    // Configurazione del MAC e PHY Ethernet
    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
   
   
    ESP_LOGI(TAG, "Log 1");

    // Inizializzazione del MAC
    esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
   
   
    ESP_LOGI(TAG, "Log 2");

    // Inizializzazione del PHY (LAN8720 รจ usato come esempio)
    esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
   
   
    ESP_LOGI(TAG, "Log 3");

    // Configurazione del driver Ethernet
    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
   
   
    ESP_LOGI(TAG, "Log 4");
   
    // Installazione del driver Ethernet
    esp_eth_handle_t eth_handle = NULL;
    ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));

   
    ESP_LOGI(TAG, "Log 5");

    // Registrazione del gestore degli eventi Ethernet
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));

    ESP_LOGI(TAG, "Log 6");
    // Avvio dell'Ethernet
    ESP_ERROR_CHECK(esp_eth_start(eth_handle));
   
    ESP_LOGI(TAG, "Log 7");
}

This is the log:
QuoteI (154) psram: This chip is ESP32-D0WD
I (155) spiram: Found 64MBit SPI RAM device
I (155) spiram: SPI RAM mode: flash 40m sram 40m
I (158) spiram: PSRAM initialized, cache is in low/high (2-core) mode.
I (165) cpu_start: Pro cpu up.
I (169) cpu_start: Starting app cpu, entry point is 0x40081244
I (0) cpu_start: App cpu up.
I (190) cpu_start: Pro cpu start user code
I (190) cpu_start: cpu freq: 240000000
I (190) cpu_start: Application information:
I (195) cpu_start: Project name:    MrLightRGB
I (200) cpu_start: App version:      cf1d79fb-dirty
I (206) cpu_start: Compile time:    Aug 19 2024 15:06:03
I (212) cpu_start: ELF file SHA256:  2d0b2f1861f4ea35...
I (218) cpu_start: ESP-IDF:          cf1d79fb-dirty
I (224) heap_init: Initializing. RAM available for dynamic allocation:
I (230) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (237) heap_init: At 3FFB3598 len 0002CA68 (178 KiB): DRAM
I (243) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (249) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (256) heap_init: At 4008E380 len 00011C80 (71 KiB): IRAM
I (262) spiram: Adding pool of 4096K of external SPI memory to heap allocator
I (271) spi_flash: detected chip: gd
I (274) spi_flash: flash io: dio
W (278) spi_flash: Detected size(16384k) larger than the size in the binary image header(8192k). Using the size in the binary image header.
I (292) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (307) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations
I (316) eth_example: Log

And this is my sdkconfig:
Quote#
# Ethernet
#
CONFIG_ETH_ENABLED=y
CONFIG_ETH_USE_ESP32_EMAC=y
CONFIG_ETH_PHY_INTERFACE_RMII=y
# CONFIG_ETH_PHY_INTERFACE_MII is not set
# CONFIG_ETH_RMII_CLK_INPUT is not set
CONFIG_ETH_RMII_CLK_OUTPUT=y
CONFIG_ETH_RMII_CLK_OUTPUT_GPIO0=y
CONFIG_ETH_DMA_BUFFER_SIZE=512
CONFIG_ETH_DMA_RX_BUFFER_NUM=10
CONFIG_ETH_DMA_TX_BUFFER_NUM=10
# CONFIG_ETH_USE_SPI_ETHERNET is not set
# CONFIG_ETH_USE_OPENETH is not set
# end of Ethernet

Values for MDC, MDIO,ADRR are default one and they are the same you show me.
I'm not using Arduino IDE, but directly native esp-idf.
Lan seems to work because i see orange led blinking, so i suppose it's good, but i cannot use any kind of log!

LubOlimex

Just test with some ping demo.
Technical support and documentation manager at Olimex

leonardo.zambonelli

Probably i was not able to explain.
I have no console output so i don't know what's happening., don't know if it  manages to take an ip, cannot log ANYTHING. (so i cannot do ping)
Ad i run this code:
gpio_pad_select_gpio(PHY_POWER_PIN);
gpio_set_direction(PHY_POWER_PIN, GPIO_MODE_OUTPUT);
gpio_set_level(PHY_POWER_PIN, 1); // Imposta il livello logico alto per accendere il PHY
console output, using ESP_LOGI, stop working!!
I know the problem is connected to the clock connected to gPIO, but i don't know how to figure out

leonardo.zambonelli

More info:
using your example downloaded from your github in arduino IDE:

/*

This sketch shows the Ethernet event usage with ESP32-POE2. Compile and download the code and open serial monitor to check if the boards connects to google.com.

Remember to select proper board in the board selector.
ESP32-POE2 has WROVER module with PSRAM. You should have PSRAM enabled! From Tools -> PSRAM -> Enabled.
Make sure proper COM port is selected.

*/

// Important to be defined BEFORE including ETH.h for
// ETH.begin() to work.
// Example RMII LAN8720 (Olimex, etc.)
#ifndef ETH_PHY_TYPE
#define ETH_PHY_TYPE  ETH_PHY_LAN8720
#define ETH_PHY_ADDR  0
#define ETH_PHY_MDC   23
#define ETH_PHY_MDIO  18
#define ETH_PHY_POWER 12
#define ETH_CLK_MODE  ETH_CLOCK_GPIO0_OUT
#endif

#include <ETH.h>

static bool eth_connected = false;

// WARNING: onEvent is called from a separate FreeRTOS task (thread)!
void onEvent(arduino_event_id_t event) {
  switch (event) {
    case ARDUINO_EVENT_ETH_START:
      Serial.println("ETH Started");
      // The hostname must be set after the interface is started, but needs
      // to be set before DHCP, so set it from the event handler thread.
      ETH.setHostname("esp32-ethernet");
      break;
    case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break;
    case ARDUINO_EVENT_ETH_GOT_IP:
      Serial.println("ETH Got IP");
      Serial.println(ETH);
      eth_connected = true;
      break;
    case ARDUINO_EVENT_ETH_LOST_IP:
      Serial.println("ETH Lost IP");
      eth_connected = false;
      break;
    case ARDUINO_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      break;
    case ARDUINO_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      eth_connected = false;
      break;
    default: break;
  }
}

void testClient(const char *host, uint16_t port) {
  Serial.print("\nconnecting to ");
  Serial.println(host);

  NetworkClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    return;
  }
  client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
  while (client.connected() && !client.available());
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("closing connection\n");
  client.stop();
}

void setup() {
  Serial.begin(115200);
  Serial.println("1\n");
  Network.onEvent(onEvent);
  ETH.begin();
  Serial.println("2\n");
}

void loop() {
  if (eth_connected) {
    testClient("google.com", 80);
  }
  delay(1000);
}

I can see the log of line
Serial.println("1\n");
but not the line:
Serial.println("2\n");


This is what i see:
17:36:29.795 -> ============ Before Setup End ============
17:36:29.890 -> 1
17:36:29.890 ->
17:36:29.890 ->    (here there are a lot of "square" so i suppose are not printable chars)

As u can see, console stops working in the same way it happens in my example

LubOlimex

What board did you select in the board selector in Arduino IDE?

Did you enable PSRAM from the board selector?
Technical support and documentation manager at Olimex

leonardo.zambonelli

#7
PSRam Enabled (i have a Wroover)
Olimex ESP32-POE  (poe2 not present)

But that's not the point!
I do not want to use Arduino, but ESp-IDF sdk

Is it possible to have an esp-idf simple project for example, instead of having only arduino one?

LubOlimex

The "1" and "2" you added are hard to see since they are not in the loop. You never reach "2" because you probably don't have proper Ethernet connection, and even then it loops so fast that you'd go past it. Better place it around the loop function.

Squares you see are just boot info characters received at wrong serial settings.

If you have Ethernet cable connected with DHCP and internet connection you would see loop:

11:30:42.222 -> connecting to google.com
11:30:42.477 -> HTTP/1.1 301 Moved Permanently
11:30:42.477 -> Location: http://www.google.com/
11:30:42.477 -> Content-Type: text/html; charset=UTF-8
11:30:42.477 -> Content-Security-Policy-Report-Only: object-src 'none';base-uri 'self';script-src 'nonce-z5fJZo8eEG1J2aleiqI2yg' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
11:30:42.521 -> Date: Tue, 20 Aug 2024 08:30:42 GMT
11:30:42.521 -> Expires: Thu, 19 Sep 2024 08:30:42 GMT
11:30:42.521 -> Cache-Control: public, max-age=2592000
11:30:42.521 -> Server: gws
11:30:42.521 -> Content-Length: 219
11:30:42.521 -> X-XSS-Protection: 0
11:30:42.521 -> X-Frame-Options: SAMEORIGIN
11:30:42.521 ->
11:30:42.521 -> <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
11:30:42.521 -> <TITLE>301 Moved</TITLE></HEAD><BODY>
11:30:42.521 -> <H1>301 Moved</H1>
11:30:42.521 -> The document has moved
11:30:42.521 -> <A HREF="http://www.google.com/">here</A>.
11:30:42.568 -> </BODY></HTML>
11:30:42.568 -> closing connection

About IDF example, did you try the ESP32-POE example (just change the assignments with the menuconfig):

https://github.com/OLIMEX/ESP32-POE/tree/master/SOFTWARE/ESP-IDF/ESP32_PoE_Ethernet_IDFv4.0
Technical support and documentation manager at Olimex

LubOlimex

The basic example should work after you edit the pins with menuconfig:

https://github.com/espressif/esp-idf/tree/v5.3/examples/ethernet/basic
Technical support and documentation manager at Olimex

leonardo.zambonelli

Thanks,
i didn0t found the esp-idf example before.
i will get a try asap and let u know

Thanks in advance!!

leonardo.zambonelli

Sorry to bother u, but does not work with 4.X example (Ethernet basic).
This is my config:
Ethernet Type: Internal EMAC
Ethernet PHY Device --> LAN8720
MDC GPIO: 23
MDIO GPIO: 18
Reset:-1
PhyAdress: 0

PhyInterface: RMII
RMII Clock mode: Output RMII clock from internal
GPIO0 Clock: checked (it's marked as experimental)

There are no Power pin selection, so it did not started. Added lines:
gpio_pad_select_gpio(12);
gpio_set_direction(12, GPIO_MODE_OUTPUT);
gpio_set_level(12, 1);

As cable is connected, led are blinking, but no log written (got_ip_event_handler is never fired). I'm sure 100% cable is good bedcause is the one tthat i use to  connect my private AccessPoint to company network

As a test i added in my main:
void myTask(void * pvParameters)
{
ESP_LOGI("Test", "Running task");
int32_t counter = 0;
for (;  ;)
{
if (counter++ > 400)
{
ESP_LOGI("Test", "Coutner: %d", counter);
counter = 0;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}

void app_main(void)
{
ESP_LOGI("Test", "Creating task");
xTaskCreatePinnedToCore(&myTask, "RGBEngine Task", configMINIMAL_STACK_SIZE * 5, NULL, 18, NULL, 1);

vTaskDelay(pdMS_TO_TICKS(1000));

In the log i see just "Creating task" and "Running task".
If i comment everything but task creation i see my log in the console written by loop
My board is your Olimex-POE2.
Is there anything i have to change in my menuconfig?

I have an already existing projext, and i do not move to esp-idf 5.X

LubOlimex

#12
You did well, maybe just put the PHY power pin as reset aka Reset:12

Just do the same config via the menuconfig with the default example for latest version of ESP-IDF:

https://github.com/espressif/esp-idf/tree/v5.3/examples/ethernet/basic

I tested it today and it seems to work here:

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x1b (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:7176
load:0x40078000,len:15564
ho 0 tail 12 room 4
load:0x40080400,len:4
0x40080400: _init at ??:?

load:0x40080404,len:3904
entry 0x40080640
I (31) boot: ESP-IDF v5.3 2nd stage bootloader
I (31) boot: compile time Aug 21 2024 13:30:36
I (31) boot: Multicore bootloader
I (35) boot: chip revision: v3.0
I (39) boot.esp32: SPI Speed      : 40MHz
I (44) boot.esp32: SPI Mode      : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (53) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset  Length
I (69) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (76) boot:  1 phy_init        RF data          01 01 0000f000 00001000
I (84) boot:  2 factory          factory app      00 00 00010000 00100000
I (91) boot: End of partition table
I (96) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=12dd0h ( 77264) map
I (131) esp_image: segment 1: paddr=00022df8 vaddr=3ffb0000 size=02474h (  9332) load
I (134) esp_image: segment 2: paddr=00025274 vaddr=40080000 size=0ada4h ( 44452) load
I (154) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=310f0h (200944) map
I (223) esp_image: segment 4: paddr=00061118 vaddr=4008ada4 size=02a80h ( 10880) load
I (234) boot: Loaded app from partition at offset 0x10000
I (234) boot: Disabling RNG early entropy source...
I (246) cpu_start: Multicore app
I (255) cpu_start: Pro cpu start user code
I (255) cpu_start: cpu freq: 160000000 Hz
I (255) app_init: Application information:
I (258) app_init: Project name:    ethernet_basic
I (263) app_init: App version:      v5.3
I (268) app_init: Compile time:    Aug 21 2024 13:29:56
I (274) app_init: ELF file SHA256:  8b35490dc...
I (279) app_init: ESP-IDF:          v5.3
I (284) efuse_init: Min chip rev:    v0.0
I (288) efuse_init: Max chip rev:    v3.99
I (293) efuse_init: Chip rev:        v3.0
I (298) heap_init: Initializing. RAM available for dynamic allocation:
I (306) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (311) heap_init: At 3FFB3748 len 0002C8B8 (178 KiB): DRAM
I (318) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (324) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (330) heap_init: At 4008D824 len 000127DC (73 KiB): IRAM
I (338) spi_flash: detected chip: generic
I (341) spi_flash: flash io: dio
I (346) main_task: Started on CPU0
I (356) main_task: Calling app_main()
I (386) esp_eth.netif.netif_glue: 10:97:bd:e4:b5:d3
I (386) esp_eth.netif.netif_glue: ethernet attached to netif
I (1986) eth_example: Ethernet Started
I (1986) main_task: Returned from app_main()
I (1986) eth_example: Ethernet Link Up
I (1986) eth_example: Ethernet HW Addr 10:97:bd:e4:b5:d3
I (3986) esp_netif_handlers: eth ip: 192.168.0.165, mask: 255.255.255.0, gw: 192.168.0.1
I (3986) eth_example: Ethernet Got IP Address
I (3986) eth_example: ~~~~~~~~~~~
I (3986) eth_example: ETHIP:192.168.0.165
I (3996) eth_example: ETHMASK:255.255.255.0
I (3996) eth_example: ETHGW:192.168.0.1
I (4006) eth_example: ~~~~~~~~~~~
Technical support and documentation manager at Olimex

leonardo.zambonelli

May you provide me a complete example project using 4.3 esp-idf, please?

leonardo.zambonelli

#14
Just tried Arduino example from your github:
/*

This sketch shows the Ethernet event usage with ESP32-POE2. Compile and download the code and open serial monitor to check if the boards connects to google.com.

Remember to select proper board in the board selector.
ESP32-POE2 has WROVER module with PSRAM. You should have PSRAM enabled! From Tools -> PSRAM -> Enabled.
Make sure proper COM port is selected.

*/

// Important to be defined BEFORE including ETH.h for
// ETH.begin() to work.
// Example RMII LAN8720 (Olimex, etc.)
#ifndef ETH_PHY_TYPE
#define ETH_PHY_TYPE  ETH_PHY_LAN8720
#define ETH_PHY_ADDR  0
#define ETH_PHY_MDC   23
#define ETH_PHY_MDIO  18
#define ETH_PHY_POWER 12
#define ETH_CLK_MODE  ETH_CLOCK_GPIO0_OUT
#endif

#include <ETH.h>

static bool eth_connected = false;
static int counter=0;
// WARNING: onEvent is called from a separate FreeRTOS task (thread)!
void onEvent(arduino_event_id_t event) {
  switch (event) {
    case ARDUINO_EVENT_ETH_START:
      Serial.println("ETH Started");
      // The hostname must be set after the interface is started, but needs
      // to be set before DHCP, so set it from the event handler thread.
      ETH.setHostname("esp32-ethernet");
      break;
    case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break;
    case ARDUINO_EVENT_ETH_GOT_IP:
      Serial.println("ETH Got IP");
      Serial.println(ETH);
      eth_connected = true;
      break;
    case ARDUINO_EVENT_ETH_LOST_IP:
      Serial.println("ETH Lost IP");
      eth_connected = false;
      break;
    case ARDUINO_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      break;
    case ARDUINO_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      eth_connected = false;
      break;
    default: break;
  }
}

void testClient(const char *host, uint16_t port) {
  Serial.print("\nconnecting to ");
  Serial.println(host);

  NetworkClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    return;
  }
  client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
  while (client.connected() && !client.available());
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("closing connection\n");
  client.stop();
}

void setup() {
  Serial.begin(115200);
  Serial.write("1\n");
  Network.onEvent(onEvent);
  ETH.begin();
  Serial.write("2\n");
}

void loop() {
 
  Serial.write("Counter:\n");
  if (eth_connected) {
    testClient("google.com", 80);
  }
  delay(1000);
}

CANNOT SEE line "Counter:"
If i remove ETH.Begin everything is working fine

FYI: i checked with look@lan and the board takes IP and reply to ping, so network interface is working fine, but i still have no console output.