Ethernet-WiFi bridge with ENC28J60

Palonso
Posts: 95
Joined: Tue Sep 24, 2019 8:43 pm

Ethernet-WiFi bridge with ENC28J60

Postby Palonso » Tue May 04, 2021 10:08 pm

Hi,

I'm coding an Ethernet-WiFi bridge for the ENC28J60 by mixing the eth2ap (https://github.com/espressif/esp-idf/tr ... net/eth2ap) and enc28j60 (https://github.com/espressif/esp-idf/tr ... t/enc28j60) example codes, and there is a certain code line that caught my attention.

In the ENC28J60 device configuration it says that you have to provide a MAC address:

Code: Select all

    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
    phy_config.phy_addr = 0;
    phy_config.autonego_timeout_ms = 0; // ENC28J60 doesn't support auto-negotiation
    phy_config.reset_gpio_num = -1;     // ENC28J60 doesn't have a pin to reset internal PHY
    esp_eth_phy_t *phy = esp_eth_phy_new_enc28j60(&phy_config);

    esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
    eth_config.stack_input = pkt_eth2wifi;
    ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config, &eth_handle));
    esp_eth_ioctl(eth_handle, ETH_CMD_S_PROMISCUOUS, (void *)true);

    /*  ENC28J60 doesn't burn any factory MAC address, we need to set it manually.
        02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
    */
    mac->set_addr(mac, (uint8_t[]) {
        0x02, 0x00, 0x00, 0x12, 0x34, 0x56
    });

    /* attach Ethernet driver to TCP/IP stack */
    ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
    /* start Ethernet driver state machine */
    ESP_ERROR_CHECK(esp_eth_start(eth_handle));
Which I would like to use the Ethernet MAC address of the ESP32 (should I add esp_read_mac()+3 to set it?)

but what caught my attention was the next piece of code:

Code: Select all

static void eth_event_handler(void *arg, esp_event_base_t event_base,
                              int32_t event_id, void *event_data)
{
    uint8_t mac_addr[6] = {0};
    /* we can get the ethernet driver handle from event data */
    esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;

    switch (event_id) {
    case ETHERNET_EVENT_CONNECTED:
        esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
        ESP_LOGI(TAG, "Ethernet Link Up");
        ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
                 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
        break;
    case ETHERNET_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "Ethernet Link Down");
        break;
    case ETHERNET_EVENT_START:
        ESP_LOGI(TAG, "Ethernet Started");
        break;
    case ETHERNET_EVENT_STOP:
        ESP_LOGI(TAG, "Ethernet Stopped");
        break;
    default:
        break;
    }
}
from the enc28j60 example

and:

Code: Select all

// Event handler for 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 Link Up");
        s_ethernet_is_connected = true;
        esp_eth_ioctl(s_eth_handle, ETH_CMD_G_MAC_ADDR, s_eth_mac);
        esp_wifi_set_mac(WIFI_IF_AP, s_eth_mac);
        ESP_ERROR_CHECK(esp_wifi_start());
        break;
    case ETHERNET_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "Ethernet Link Down");
        s_ethernet_is_connected = false;
        ESP_ERROR_CHECK(esp_wifi_stop());
        break;
    case ETHERNET_EVENT_START:
        ESP_LOGI(TAG, "Ethernet Started");
        break;
    case ETHERNET_EVENT_STOP:
        ESP_LOGI(TAG, "Ethernet Stopped");
        break;
    default:
        break;
    }
}
from the eth2ap example

From the enc28j60 example the handler gets the MAC address of what? the device it has to forward packets? or its own mac address?
since that address is "globally" defined in the eth2ap example code, it has to be its own MAC address, right? because its defining its wifi MAC address as that one.

I would appreciate any explanation of this.

Best regards,
P

pdemianczuk
Posts: 28
Joined: Thu May 30, 2019 12:05 pm

Re: Ethernet-WiFi bridge with ENC28J60

Postby pdemianczuk » Fri May 14, 2021 11:13 am

Hi, I think you should use functuon esp_read_mac(...) from <esp_system.h> module.
you migh use the array of mac like a globall value or include appropriate function.

Code: Select all

uchar mac_base[6] = {0};
esp_read_mac(mac_base,0);
if you were read/check this value (for some logs or something) in appropriate way, you would remember to conversion digit to hex or digit to string.

but to init eth it would be fine in mac_base version.

syraxxl
Posts: 1
Joined: Mon Jun 21, 2021 2:26 pm

Re: Ethernet-WiFi bridge with ENC28J60

Postby syraxxl » Mon Jun 21, 2021 2:28 pm

I think the right way is to use separate MAC addresses for ETH and WiFi. You can validate this by connecting to standart router with lan cable and wifi at the same time.

Who is online

Users browsing this forum: No registered users and 98 guests