Skip to content

Commit

Permalink
Add static configuration for ethernet
Browse files Browse the repository at this point in the history
  • Loading branch information
whc2001 committed Jun 8, 2024
1 parent 08b96b3 commit c39429c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion components/eth_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
idf_component_register(SRCS "eth_interface.c"
INCLUDE_DIRS "include"
REQUIRES driver esp_eth esp_netif)
REQUIRES driver esp_eth esp_netif lwip)
36 changes: 33 additions & 3 deletions components/eth_interface/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,49 @@ menu "Snapclient Ethernet Configuration"
Set the second SPI Ethernet module PHY address according your board schematic.
endif # SNAPCLIENT_USE_SPI_ETHERNET

# Clock enable GPIO
config SNAPCLIENT_ETH_CLOCK_ENABLE_GPIO
depends on SNAPCLIENT_USE_INTERNAL_ETHERNET || SNAPCLIENT_USE_SPI_ETHERNET
int "PHY Clock Enable GPIO"
int "PHY clock enable GPIO"
default 5
help
GPIO number to control ethernet PHY's clock.
Some PHY may have constant clock output even during reset, which might cause boot failure when using clock input
mode with GPIO0. This GPIO will be configured after boot.
mode with GPIO0. This GPIO will be configured after boot.

config SNAPCLIENT_ETH_CLOCK_ENABLE_ACTIVE_LOW
depends on SNAPCLIENT_USE_INTERNAL_ETHERNET || SNAPCLIENT_USE_SPI_ETHERNET
bool "PHY Clock Enable active LOW"
bool "PHY clock enable GPIO active LOW"
default false
help
Output LOW instead of HIGH on eithernet PHY clock enable pin for enable state.

# Static configuration
config SNAPCLIENT_ETH_USE_STATIC_CONF
depends on SNAPCLIENT_USE_INTERNAL_ETHERNET || SNAPCLIENT_USE_SPI_ETHERNET
bool "Use static ethernet configuration"
default false
help
Use static configuration instead of DHCP for ethernet. Currently this only works with internal PHY with a single interface.

config SNAPCLIENT_ETH_STATIC_IP
depends on SNAPCLIENT_ETH_USE_STATIC_CONF
string "IP address"
default "192.168.1.123"
help
Set the static IP address for the ethernet interface.

config SNAPCLIENT_ETH_STATIC_NETMASK
depends on SNAPCLIENT_ETH_USE_STATIC_CONF
string "Netmask"
default "255.255.255.0"
help
Set the static netmask for the ethernet interface.

config SNAPCLIENT_ETH_STATIC_GATEWAY
depends on SNAPCLIENT_ETH_USE_STATIC_CONF
string "Gateway"
default "192.168.1.1"
help
Set the static gateway for the ethernet interface.
endmenu
37 changes: 32 additions & 5 deletions components/eth_interface/eth_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "lwip/ip4_addr.h"
#include "sdkconfig.h"
#if CONFIG_SNAPCLIENT_USE_SPI_ETHERNET
#include "driver/spi_master.h"
#endif

static const char *TAG = "snapclient_eth_init";


/* The event group allows multiple bits for each event, but we only care about
* two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define ETH_CONNECTED_BIT BIT0
#define ETH_FAIL_BIT BIT1

static bool connected = false, got_ip = false;
static EventGroupHandle_t s_eth_event_group;

#if CONFIG_SNAPCLIENT_SPI_ETHERNETS_NUM
Expand Down Expand Up @@ -66,6 +67,16 @@ typedef struct {
uint8_t *mac_addr;
} spi_eth_module_config_t;

static void set_eth_events() {
if (connected && got_ip) {
xEventGroupSetBits(s_eth_event_group, ETH_CONNECTED_BIT);
xEventGroupClearBits(s_eth_event_group, ETH_FAIL_BIT);
} else if (!connected) {
xEventGroupSetBits(s_eth_event_group, ETH_FAIL_BIT);
xEventGroupClearBits(s_eth_event_group, ETH_CONNECTED_BIT);
}
}

#if CONFIG_SNAPCLIENT_USE_INTERNAL_ETHERNET
/**
* @brief Internal ESP32 Ethernet initialization
Expand Down Expand Up @@ -362,10 +373,13 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
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]);
connected = true;
set_eth_events();
break;
case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Ethernet Link Down");
xEventGroupSetBits(s_eth_event_group, ETH_FAIL_BIT);
connected = false;
set_eth_events();
break;
case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet Started");
Expand All @@ -391,11 +405,14 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");

xEventGroupSetBits(s_eth_event_group, ETH_CONNECTED_BIT);
got_ip = true;
set_eth_events();
}

/** Init function that exposes to the main application */
void eth_init(void) {
s_eth_event_group = xEventGroupCreate();

// Initialize Ethernet driver
uint8_t eth_port_cnt = 0;
esp_eth_handle_t *eth_handles;
Expand All @@ -416,6 +433,18 @@ void eth_init(void) {
// Attach Ethernet driver to TCP/IP stack
ESP_ERROR_CHECK(
esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[0])));

#if CONFIG_SNAPCLIENT_ETH_USE_STATIC_CONF
// Use static configuration instead of DHCP
esp_netif_ip_info_t info_t;
memset(&info_t, 0, sizeof(esp_netif_ip_info_t));
ip4addr_aton(CONFIG_SNAPCLIENT_ETH_STATIC_IP, &info_t.ip);
ip4addr_aton(CONFIG_SNAPCLIENT_ETH_STATIC_GATEWAY, &info_t.gw);
ip4addr_aton(CONFIG_SNAPCLIENT_ETH_STATIC_NETMASK, &info_t.netmask);
esp_netif_dhcpc_stop(eth_netif);
esp_netif_set_ip_info(eth_netif, &info_t);
#endif

} else {
// Use ESP_NETIF_INHERENT_DEFAULT_ETH when multiple Ethernet interfaces are
// used and so you need to modify esp-netif configuration parameters for
Expand Down Expand Up @@ -456,8 +485,6 @@ void eth_init(void) {
/* Waiting until either the connection is established (ETH_CONNECTED_BIT) or
* connection failed for the maximum number of re-tries (ETH_FAIL_BIT). The
* bits are set by event_handler() (see above) */
s_eth_event_group = xEventGroupCreate();
// EventBits_t bits =
xEventGroupWaitBits(s_eth_event_group, ETH_CONNECTED_BIT, pdFALSE, pdFALSE,
portMAX_DELAY);
}

0 comments on commit c39429c

Please sign in to comment.