unnable to run eth2wifi , need help

ht_main1
Posts: 12
Joined: Sun Oct 06, 2019 6:08 pm

unnable to run eth2wifi , need help

Postby ht_main1 » Sat Nov 02, 2019 6:51 am

i am trying to use eth2ap as eth2sta, i modified the given example to eth2sta, i had successfully run eth2ap example and wanted to modify it for STA, i also read the eth2wifi example in IOT-SOlutions, but i couldnt run it on my esp-idf version, hence i had need to modify the give example from eth2ap to eth2sta,

i believe i have done necessary changes rightly still i am unable to get it working,
following is modified code

Code: Select all

/* eth2ap (Ethernet to Wi-Fi AP packet forwarding) Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_eth.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "esp_private/wifi.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

static const char *TAG = "eth_example";
static esp_eth_handle_t s_eth_handle = NULL;
static xQueueHandle flow_control_queue = NULL;
static bool s_sta_is_connected = false;
static bool s_ethernet_is_connected = false;
static uint8_t s_eth_mac[6];

#define FLOW_CONTROL_QUEUE_TIMEOUT_MS (100)
#define FLOW_CONTROL_QUEUE_LENGTH (40)
#define FLOW_CONTROL_WIFI_SEND_TIMEOUT_MS (100)

typedef struct {
    void *packet;
    uint16_t length;
} flow_control_msg_t;

// Forward packets from Wi-Fi to Ethernet
static esp_err_t pkt_wifi2eth(void *buffer, uint16_t len, void *eb)
{
    if (s_ethernet_is_connected) {
        if (esp_eth_transmit(s_eth_handle, buffer, len) != ESP_OK) {
            ESP_LOGE(TAG, "Ethernet send packet failed");
        }
    }
    esp_wifi_internal_free_rx_buffer(eb);
    return ESP_OK;
}

// Forward packets from Ethernet to Wi-Fi
// Note that, Ethernet works faster than Wi-Fi on ESP32,
// so we need to add an extra queue to balance their speed difference.
static esp_err_t pkt_eth2wifi(esp_eth_handle_t eth_handle, uint8_t *buffer, uint32_t len)
{
    esp_err_t ret = ESP_OK;
    flow_control_msg_t msg = {
        .packet = buffer,
        .length = len
    };
    if (xQueueSend(flow_control_queue, &msg, pdMS_TO_TICKS(FLOW_CONTROL_QUEUE_TIMEOUT_MS)) != pdTRUE) {
        ESP_LOGE(TAG, "send flow control message failed or timeout");
        free(buffer);
        ret = ESP_FAIL;
    }
    return ret;
}

// This task will fetch the packet from the queue, and then send out through Wi-Fi.
// Wi-Fi handles packets slower than Ethernet, we might add some delay between each transmitting.
static void eth2wifi_flow_control_task(void *args)
{
    flow_control_msg_t msg;
    int res = 0;
    uint32_t timeout = 0;
    while (1) {
        if (xQueueReceive(flow_control_queue, &msg, pdMS_TO_TICKS(FLOW_CONTROL_QUEUE_TIMEOUT_MS)) == pdTRUE) {
            timeout = 0;
            if (s_sta_is_connected && msg.length) {
                do {
                    vTaskDelay(pdMS_TO_TICKS(timeout));
                    timeout += 2;
                    res = esp_wifi_internal_tx(ESP_IF_WIFI_AP, msg.packet, msg.length);
                } while (res && timeout < FLOW_CONTROL_WIFI_SEND_TIMEOUT_MS);
                if (res != ESP_OK) {
                    ESP_LOGE(TAG, "WiFi send packet failed: %d", res);
                }
            }
            free(msg.packet);
        }
    }
    vTaskDelete(NULL);
}

// 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;
    }
}

// Event handler for Wi-Fi
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    switch (event_id) {
    case WIFI_EVENT_AP_STACONNECTED:
        ESP_LOGI(TAG, "Wi-Fi AP got a station connected");
        s_sta_is_connected = true;
        esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, pkt_wifi2eth);
        break;
    case WIFI_EVENT_AP_STADISCONNECTED:
        ESP_LOGI(TAG, "Wi-Fi AP got a station disconnected");
        s_sta_is_connected = false;
        esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, NULL);
        break;
    default:
        break;
    }
}

static void initialize_ethernet(void)
{
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler, NULL));
    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
// #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
     esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
// #if CONFIG_EXAMPLE_ETH_PHY_IP101
    // esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
// #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
    // esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
// #elif CONFIG_EXAMPLE_ETH_PHY_LAN8720
    // esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
// #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
    esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
// #endif
// #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET
    // gpio_install_isr_service(0);
    // spi_device_handle_t spi_handle = NULL;
    // spi_bus_config_t buscfg = {
        // .miso_io_num = CONFIG_EXAMPLE_ETH_MISO_GPIO,
        // .mosi_io_num = CONFIG_EXAMPLE_ETH_MOSI_GPIO,
        // .sclk_io_num = CONFIG_EXAMPLE_ETH_SCLK_GPIO,
        // .quadwp_io_num = -1,
        // .quadhd_io_num = -1,
    // };
    // ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1));
    // spi_device_interface_config_t devcfg = {
        // .command_bits = 1,
        // .address_bits = 7,
        // .mode = 0,
        // .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
        // .spics_io_num = CONFIG_EXAMPLE_ETH_CS_GPIO,
        // .queue_size = 20
    // };
    // ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
    // /* dm9051 ethernet driver is based on spi driver */
    // eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
    // esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
    // esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config);
// #endif
    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
    config.stack_input = pkt_eth2wifi;
    ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
    esp_eth_ioctl(s_eth_handle, ETH_CMD_S_PROMISCUOUS, (void *)true);
}

static void initialize_wifi(void)
{
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL));
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(tcpip_adapter_clear_default_wifi_handlers());
    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
    wifi_config_t wifi_config = {
        .ap = {
            .ssid = CONFIG_EXAMPLE_WIFI_SSID,
            .ssid_len = strlen(CONFIG_EXAMPLE_WIFI_SSID),
            .password = CONFIG_EXAMPLE_WIFI_PASSWORD,
            .max_connection = CONFIG_EXAMPLE_MAX_STA_CONN,
            .authmode = WIFI_AUTH_WPA_WPA2_PSK
        },
    };
    if (strlen(CONFIG_EXAMPLE_WIFI_PASSWORD) == 0) {
        wifi_config.ap.authmode = WIFI_AUTH_OPEN;
    }
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
}

static esp_err_t initialize_flow_control(void)
{
    flow_control_queue = xQueueCreate(FLOW_CONTROL_QUEUE_LENGTH, sizeof(flow_control_msg_t));
    if (!flow_control_queue) {
        ESP_LOGE(TAG, "create flow control queue failed");
        return ESP_FAIL;
    }
    BaseType_t ret = xTaskCreate(eth2wifi_flow_control_task, "flow_ctl", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL);
    if (ret != pdTRUE) {
        ESP_LOGE(TAG, "create flow control task failed");
        return ESP_FAIL;
    }
    return ESP_OK;
}

void app_main(void)
{
	int tmp=0;
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    ESP_ERROR_CHECK(initialize_flow_control());

	
	gpio_pad_select_gpio(GPIO_NUM_14);
    gpio_pad_select_gpio(GPIO_NUM_2);
    gpio_pad_select_gpio(GPIO_NUM_16);
    
	gpio_set_direction(GPIO_NUM_16 , GPIO_MODE_OUTPUT );
	gpio_set_direction(GPIO_NUM_2 , GPIO_MODE_OUTPUT );
	gpio_set_direction(GPIO_NUM_14 , GPIO_MODE_OUTPUT );
    vTaskDelay(10 / portTICK_RATE_MS);
	printf("Set OSC EN  = 1");
	tmp = 1;
	while(++tmp < 10){
		printf("init GPIO\n\r");
		gpio_set_level(GPIO_NUM_14, 1);
		gpio_set_level(GPIO_NUM_16, 0);
		gpio_set_level(GPIO_NUM_2, 1);
		vTaskDelay(100 / portTICK_RATE_MS);
		gpio_set_level(GPIO_NUM_2, 0);
		gpio_set_level(GPIO_NUM_16, 1);
		vTaskDelay(100 / portTICK_RATE_MS);
		printf("get gpio14 %d\n\r", gpio_get_level(GPIO_NUM_14));
	}
    initialize_ethernet();
    initialize_wifi();
}
also i have attached event log,
[0;32mI (13) boot: ESP-IDF v4.1-dev-369-g4dac7c7df-dirty 2nd stage bootloader[0m
[0;32mI (13) boot: compile time 17:25:48[0m
[0;32mI (13) boot: Enabling RNG early entropy source...[0m
[0;32mI (18) boot: SPI Speed : 40MHz[0m
[0;32mI (23) boot: SPI Mode : DIO[0m
[0;32mI (27) boot: SPI Flash Size : 4MB[0m
[0;32mI (31) boot: Partition Table:[0m
[0;32mI (34) boot: ## Label Usage Type ST Offset Length[0m
[0;32mI (42) boot: 0 nvs WiFi data 01 02 00009000 00006000[0m
[0;32mI (49) boot: 1 phy_init RF data 01 01 0000f000 00001000[0m
[0;32mI (56) boot: 2 factory factory app 00 00 00010000 00100000[0m
[0;32mI (64) boot: End of partition table[0m
[0;32mI (68) boot_comm: mismatch chip revision, expect 1, found 0[0m
[0;32mI (74) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x162f4 ( 90868) map[0m
[0;32mI (116) esp_image: segment 1: paddr=0x0002631c vaddr=0x3ffb0000 size=0x031e4 ( 12772) load[0m
[0;32mI (121) esp_image: segment 2: paddr=0x00029508 vaddr=0x40080000 size=0x00400 ( 1024) load[0m
[0;32mI (123) esp_image: segment 3: paddr=0x00029910 vaddr=0x40080400 size=0x06700 ( 26368) load[0m
[0;32mI (143) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x67584 (423300) map[0m
[0;32mI (294) esp_image: segment 5: paddr=0x000975a4 vaddr=0x40086b00 size=0x0a208 ( 41480) load[0m
[0;32mI (322) boot: Loaded app from partition at offset 0x10000[0m
[0;32mI (323) boot: Disabling RNG early entropy source...[0m
[0;32mI (323) cpu_start: Pro cpu up.[0m
[0;32mI (327) cpu_start: Application information:[0m
[0;32mI (331) cpu_start: Project name: eth2ap[0m
[0;32mI (336) cpu_start: App version: 1[0m
[0;32mI (341) cpu_start: Compile time: Nov 1 2019 17:44:06[0m
[0;32mI (347) cpu_start: ELF file SHA256: 62b21cc0512767cf...[0m
[0;32mI (353) cpu_start: ESP-IDF: v4.1-dev-369-g4dac7c7df-dirty[0m
[0;32mI (360) cpu_start: Starting app cpu, entry point is 0x4008117c[0m
[0;32mI (0) cpu_start: App cpu up.[0m
[0;32mI (370) heap_init: Initializing. RAM available for dynamic allocation:[0m
[0;32mI (377) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM[0m
[0;32mI (383) heap_init: At 3FFB8858 len 000277A8 (157 KiB): DRAM[0m
[0;32mI (389) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM[0m
[0;32mI (396) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM[0m
[0;32mI (402) heap_init: At 40090D08 len 0000F2F8 (60 KiB): IRAM[0m
[0;32mI (408) cpu_start: Pro cpu start user code[0m
[0;32mI (427) spi_flash: detected chip: generic[0m
[0;32mI (427) spi_flash: flash io: dio[0m
[0;32mI (427) cpu_start: Starting scheduler on PRO CPU.[0m
[0;32mI (0) cpu_start: Starting scheduler on APP CPU.[0m
[0;32mI (3712) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
[0;32mI (3722) eth_example: Ethernet Started[0m
I (3732) wifi: wifi driver task: 3ffc34e4, prio:23, stack:3584, core=0
[0;32mI (3732) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
[0;32mI (3732) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
I (3762) wifi: wifi firmware version: c01c829
I (3762) wifi: config NVS flash: enabled
I (3762) wifi: config nano formating: disabled
I (3762) wifi: Init dynamic tx buffer num: 32
I (3762) wifi: Init data frame dynamic rx buffer num: 32
I (3772) wifi: Init management frame dynamic rx buffer num: 32
I (3772) wifi: Init management short buffer num: 32
I (3782) wifi: Init static rx buffer size: 1600
I (3782) wifi: Init static rx buffer num: 10
I (3792) wifi: Init dynamic rx buffer num: 32
[0;32mI (9692) eth_example: Ethernet Link Up[0m
[0;32mI (9782) phy: phy_version: 4102, 2fa7a43, Jul 15 2019, 13:06:06, 0, 0[0m
I (9782) wifi: mode : sta (24:0a:c4:24:fa:ff)
SYSTEM_EVENT_STA_START

I (9902) wifi: new:<6,0>, old:<1,0>, ap:<255,255>, sta:<6,0>, prof:1
I (10442) wifi: state: init -> auth (b0)
I (10442) wifi: state: auth -> assoc (0)
I (10462) wifi: state: assoc -> run (10)
I (10742) wifi: connected with heaptronix, channel 6, BW20, bssid = 78:cd:8e:8b:6b:3e
I (10742) wifi: pm start, type: 1

SYSTEM_EVENT_STA_CONNECTED
kindly let me know any mistake in code, looking forward for guidance,

regards
sushant

ht_main1
Posts: 12
Joined: Sun Oct 06, 2019 6:08 pm

Re: unnable to run eth2wifi , need help

Postby ht_main1 » Sun Nov 03, 2019 7:07 am

Code: Select all

 // macffr.pmode = EMAC_PROMISCUOUS_DISABLE;
	macffr.pmode = EMAC_PROMISCUOUS_ENABLE;
as recommended in this post, i tried enabling the promiscuous mode in file,
but its still not working

ht_main1
Posts: 12
Joined: Sun Oct 06, 2019 6:08 pm

Re: unnable to run eth2wifi , need help

Postby ht_main1 » Sun Nov 03, 2019 10:48 am

ht_main1 wrote:
Sun Nov 03, 2019 7:07 am

Code: Select all

 // macffr.pmode = EMAC_PROMISCUOUS_DISABLE;
	macffr.pmode = EMAC_PROMISCUOUS_ENABLE;
as recommended in this post, i tried enabling the promiscuous mode in file,
but its still not working
the post is not helpful as

Code: Select all

    esp_eth_ioctl(s_eth_handle, ETH_CMD_S_PROMISCUOUS, (void *)true);
the same enables promiscuous mode

WiFive
Posts: 2785
Joined: Tue Dec 01, 2015 7:35 am

Re: unnable to run eth2wifi , need help

Postby WiFive » Sun Nov 03, 2019 10:50 am

The code you posted does not even set sta mode

ht_main1
Posts: 12
Joined: Sun Oct 06, 2019 6:08 pm

Re: unnable to run eth2wifi , need help

Postby ht_main1 » Sun Nov 03, 2019 11:37 am

WiFive wrote:
Sun Nov 03, 2019 10:50 am
The code you posted does not even set sta mode
thanks for giving your time and inputs

Sorry for inconvenience i had 2 files opened at same instance,
following is code for STA

Code: Select all

/* eth2ap (Ethernet to Wi-Fi AP packet forwarding) Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_eth.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "esp_private/wifi.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

static const char *TAG = "eth_example";
static esp_eth_handle_t s_eth_handle = NULL;
static xQueueHandle flow_control_queue = NULL;
static bool s_sta_is_connected = false;
static bool wifi_is_connected = false; // for STA
static bool s_ethernet_is_connected = false;
static uint8_t s_eth_mac[6];

#define FLOW_CONTROL_QUEUE_TIMEOUT_MS (100)
#define FLOW_CONTROL_QUEUE_LENGTH (40)
#define FLOW_CONTROL_WIFI_SEND_TIMEOUT_MS (100)

typedef struct {
    void *packet;
    uint16_t length;
} flow_control_msg_t;

void printHEX(char *buffer, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%02X", buffer[i]);
	}
	printf("\n[i]\n");
}

// Forward packets from Wi-Fi to Ethernet
static esp_err_t pkt_wifi2eth(void *buffer, uint16_t len, void *eb)
{
	ESP_LOGI(TAG, "wifi-2-eth");
    if (s_ethernet_is_connected) {
		printf("[Wi-Fi to Ethernet]\n\r");
		printHEX(buffer, len);
        if (esp_eth_transmit(s_eth_handle, buffer, len) != ESP_OK) {
        }
    }
    esp_wifi_internal_free_rx_buffer(eb);
    return ESP_OK;
}

// Forward packets from Ethernet to Wi-Fi
// Note that, Ethernet works faster than Wi-Fi on ESP32,
// so we need to add an extra queue to balance their speed difference.
static esp_err_t pkt_eth2wifi(esp_eth_handle_t eth_handle, uint8_t *buffer, uint32_t len)
{
    esp_err_t ret = ESP_OK;
    flow_control_msg_t msg = {
        .packet = buffer,
        .length = len
    };
    if (xQueueSend(flow_control_queue, &msg, pdMS_TO_TICKS(FLOW_CONTROL_QUEUE_TIMEOUT_MS)) != pdTRUE) {
        ESP_LOGI(TAG, "send flow control message failed or timeout");
        free(buffer);
        ret = ESP_FAIL;
    }
    return ret;
}

// This task will fetch the packet from the queue, and then send out through Wi-Fi.
// Wi-Fi handles packets slower than Ethernet, we might add some delay between each transmitting.
static void eth2wifi_flow_control_task(void *args)
{
    flow_control_msg_t msg;
    int res = 0;
    uint32_t timeout = 0;
    while (1) {
        if (xQueueReceive(flow_control_queue, &msg, pdMS_TO_TICKS(FLOW_CONTROL_QUEUE_TIMEOUT_MS)) == pdTRUE) {
            timeout = 0;
            if ((wifi_is_connected || s_sta_is_connected )&& msg.length) {
				ESP_LOGI(TAG, "wifi is conneceted");
                do {
                    vTaskDelay(pdMS_TO_TICKS(timeout));
                    timeout += 2;
					printf("[Ethernet 2 Wi-Fi.]\n\r");
					printHEX(msg.packet, msg.length);
                    res = esp_wifi_internal_tx(ESP_IF_WIFI_STA, msg.packet, msg.length);
                } while (res && timeout < FLOW_CONTROL_WIFI_SEND_TIMEOUT_MS);
                if (res != ESP_OK) {
                    ESP_LOGI(TAG, "WiFi send packet failed: %d", res);
                }
            }
            free(msg.packet);
        }
    }
    vTaskDelete(NULL);
}

// 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_STA, s_eth_mac);
        ESP_LOGI(TAG, "ETH_MAC:%X:%X:%X:%X:%X:%X", s_eth_mac[0],s_eth_mac[1],s_eth_mac[2],s_eth_mac[3],s_eth_mac[4],s_eth_mac[5]);
       ESP_ERROR_CHECK(esp_wifi_start());
		esp_wifi_connect();
        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;
    }
}

// Event handler for Wi-Fi
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    switch (event_id) {
    case WIFI_EVENT_AP_STACONNECTED:
        ESP_LOGI(TAG, "Wi-Fi AP got a station connected");
        s_sta_is_connected = true;
        esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, pkt_wifi2eth);
        break;
    case WIFI_EVENT_AP_STADISCONNECTED:
        ESP_LOGI(TAG, "Wi-Fi AP got a station disconnected");
        s_sta_is_connected = false;
        esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, NULL);
        break;
	case SYSTEM_EVENT_STA_START:
		printf("SYSTEM_EVENT_STA_START\r\n");
		break;

	case SYSTEM_EVENT_STA_CONNECTED:
		printf("SYSTEM_EVENT_STA_CONNECTED\r\n");
		wifi_is_connected = true;

		esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, (wifi_rxcb_t)pkt_wifi2eth);
		break;

	case SYSTEM_EVENT_STA_GOT_IP:
		printf("SYSTEM_EVENT_STA_GOT_IP\r\n");
		break;

	case SYSTEM_EVENT_STA_DISCONNECTED:
		printf("SlYSTEM_EVENT_STA_DISCONNECTED\r\n");
		wifi_is_connected = false;
		esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, NULL);
		esp_wifi_connect();
		break;	
    default:
	
        break;
    }
}

static void initialize_ethernet(void)
{
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler, NULL));
    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
// #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
     esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
// #if CONFIG_EXAMPLE_ETH_PHY_IP101
    // esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
// #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
    // esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
// #elif CONFIG_EXAMPLE_ETH_PHY_LAN8720
    // esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
// #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
    esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
// #endif
// #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET
    // gpio_install_isr_service(0);
    // spi_device_handle_t spi_handle = NULL;
    // spi_bus_config_t buscfg = {
        // .miso_io_num = CONFIG_EXAMPLE_ETH_MISO_GPIO,
        // .mosi_io_num = CONFIG_EXAMPLE_ETH_MOSI_GPIO,
        // .sclk_io_num = CONFIG_EXAMPLE_ETH_SCLK_GPIO,
        // .quadwp_io_num = -1,
        // .quadhd_io_num = -1,
    // };
    // ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1));
    // spi_device_interface_config_t devcfg = {
        // .command_bits = 1,
        // .address_bits = 7,
        // .mode = 0,
        // .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
        // .spics_io_num = CONFIG_EXAMPLE_ETH_CS_GPIO,
        // .queue_size = 20
    // };
    // ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
    // /* dm9051 ethernet driver is based on spi driver */
    // eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
    // esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
    // esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config);
// #endif
    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
    config.stack_input = pkt_eth2wifi;
    ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
    esp_eth_ioctl(s_eth_handle, ETH_CMD_S_PROMISCUOUS, (void *)true);
}

static void initialize_wifi(void)
{
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL));
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(tcpip_adapter_clear_default_wifi_handlers());
    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = "Guren Mark 3",
            .password = "sush2019",
        },
    };
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
	//ESP_ERROR_CHECK(esp_wifi_start());
	//esp_wifi_connect();
}

static esp_err_t initialize_flow_control(void)
{
    flow_control_queue = xQueueCreate(FLOW_CONTROL_QUEUE_LENGTH, sizeof(flow_control_msg_t));
    if (!flow_control_queue) {
        ESP_LOGE(TAG, "create flow control queue failed");
        return ESP_FAIL;
    }
    BaseType_t ret = xTaskCreate(eth2wifi_flow_control_task, "flow_ctl", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL);
    if (ret != pdTRUE) {
        ESP_LOGE(TAG, "create flow control task failed");
        return ESP_FAIL;
    }
    return ESP_OK;
}

void app_main(void)
{
	int tmp=0;
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    ESP_ERROR_CHECK(initialize_flow_control());

	
	gpio_pad_select_gpio(GPIO_NUM_14);
    gpio_pad_select_gpio(GPIO_NUM_2);
    gpio_pad_select_gpio(GPIO_NUM_16);
    
	gpio_set_direction(GPIO_NUM_16 , GPIO_MODE_OUTPUT );
	gpio_set_direction(GPIO_NUM_2 , GPIO_MODE_OUTPUT );
	gpio_set_direction(GPIO_NUM_14 , GPIO_MODE_OUTPUT );
    vTaskDelay(10 / portTICK_RATE_MS);
	printf("Set OSC EN  = 1");
	tmp = 1;
	while(++tmp < 10){
		printf("init GPIO\n\r");
		gpio_set_level(GPIO_NUM_14, 1);
		gpio_set_level(GPIO_NUM_16, 0);
		gpio_set_level(GPIO_NUM_2, 1);
		vTaskDelay(100 / portTICK_RATE_MS);
		gpio_set_level(GPIO_NUM_2, 0);
		gpio_set_level(GPIO_NUM_16, 1);
		vTaskDelay(100 / portTICK_RATE_MS);
		printf("get gpio14 %d\n\r", gpio_get_level(GPIO_NUM_14));
	}
    initialize_ethernet();
    initialize_wifi();
}

ht_main1
Posts: 12
Joined: Sun Oct 06, 2019 6:08 pm

Re: unnable to run eth2wifi , need help

Postby ht_main1 » Mon Nov 04, 2019 6:22 am

anyone ?

ht_main1
Posts: 12
Joined: Sun Oct 06, 2019 6:08 pm

Re: unnable to run eth2wifi , need help

Postby ht_main1 » Tue Nov 05, 2019 6:18 am

Code: Select all

        esp_eth_ioctl(s_eth_handle, ETH_CMD_G_MAC_ADDR, s_eth_mac);
        esp_wifi_set_mac(WIFI_IF_AP, s_eth_mac);
can anybody explain why we have to MAC address for AP, and how will it work in STA mode ?

ht_main1
Posts: 12
Joined: Sun Oct 06, 2019 6:08 pm

Re: unnable to run eth2wifi , need help

Postby ht_main1 » Wed Nov 06, 2019 5:51 am

anyone who can help ?

ht_main1
Posts: 12
Joined: Sun Oct 06, 2019 6:08 pm

Re: unnable to run eth2wifi , need help

Postby ht_main1 » Thu Nov 07, 2019 7:21 am

top

Who is online

Users browsing this forum: MSN [Bot] and 24 guests