Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ idf_component_register(
audio_recorder
audio_sal
audio_stream
esp_eth
esp_http_client
esp_peripherals
esp_websocket_client
Expand Down
6 changes: 4 additions & 2 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,20 @@ void app_main(void)
ESP_ERROR_CHECK(nvs_flash_init());
}

nvs_handle_t hdl_nvs;
size_t sz;

#ifdef CONFIG_WILLOW_ETHERNET
init_ethernet();
#else
nvs_handle_t hdl_nvs;
err = nvs_open("WIFI", NVS_READONLY, &hdl_nvs);
if (err != ESP_OK) {
ESP_LOGE(TAG, "failed to open NVS namespace WIFI: %s", esp_err_to_name(err));
goto err_nvs;
}

char psk[64];
size_t sz = sizeof(psk);
sz = sizeof(psk);
err = nvs_get_str(hdl_nvs, "PSK", psk, &sz);
if (err != ESP_OK) {
ESP_LOGE(TAG, "failed to get PSK from NVS namespace WIFI: %s", esp_err_to_name(err));
Expand Down
24 changes: 14 additions & 10 deletions main/net/ethernet.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include "driver/spi_master.h"
#include "esp_eth.h"
#include "esp_mac.h"

#include "network.h"
#include "shared.h"

#define WILLOW_ETHERNET_CS 10
Expand Down Expand Up @@ -80,7 +82,8 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t
esp_err_t init_ethernet(void)
{
esp_err_t ret = ESP_OK;
ESP_ERROR_CHECK(esp_event_loop_create_default());

init_sntp();

// Start Ethernet
if (lvgl_port_lock(lvgl_lock_timeout)) {
Expand Down Expand Up @@ -112,7 +115,6 @@ esp_err_t init_ethernet(void)
eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();

// Init SPI bus
spi_device_handle_t spi_handle[1] = {NULL};
spi_bus_config_t buscfg = {
.miso_io_num = WILLOW_ETHERNET_MISO,
.mosi_io_num = WILLOW_ETHERNET_MOSI,
Expand All @@ -130,6 +132,8 @@ esp_err_t init_ethernet(void)
esp_eth_mac_t *mac_spi[1];
esp_eth_phy_t *phy_spi[1];
esp_eth_handle_t eth_handle_spi[1] = {NULL};
uint8_t eth_mac[6];
ESP_ERROR_CHECK(esp_read_mac(eth_mac, ESP_MAC_ETH));

// W5500
spi_device_interface_config_t devcfg = {.command_bits = 16, // Actually it's the address phase in W5500 SPI frame
Expand All @@ -142,9 +146,8 @@ esp_err_t init_ethernet(void)
// Set SPI module Chip Select GPIO
devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio;

ESP_ERROR_CHECK(spi_bus_add_device(WILLOW_ETHERNET_SPI_BUS, &devcfg, &spi_handle[i]));
// w5500 ethernet driver is based on spi driver
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle[i]);
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(WILLOW_ETHERNET_SPI_BUS, &devcfg);

// Set remaining GPIO numbers and configuration used by the SPI module
w5500_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
Expand All @@ -159,17 +162,16 @@ esp_err_t init_ethernet(void)
esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi[i], phy_spi[i]);
ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config_spi, &eth_handle_spi[i]));

/* The SPI Ethernet module might not have a burned factory MAC address, we cat 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.
*/
ESP_ERROR_CHECK(
esp_eth_ioctl(eth_handle_spi[i], ETH_CMD_S_MAC_ADDR, (uint8_t[]){0x02, 0x00, 0x00, 0x12, 0x34, 0x56 + i}));
// The W5500 has no burned factory address. Use the Ethernet address
// derived from the ESP's base MAC instead.
ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi[i], ETH_CMD_S_MAC_ADDR, eth_mac));

// attach Ethernet driver to TCP/IP stack
ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi[i], esp_eth_new_netif_glue(eth_handle_spi[i])));
}

set_hostname(eth_netif_spi[0], ESP_MAC_ETH);

// Register user defined event handers
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
Expand All @@ -188,5 +190,7 @@ esp_err_t init_ethernet(void)
// HACK
vTaskDelay(5000 / portTICK_PERIOD_MS);

start_sntp();

return ret;
}
19 changes: 7 additions & 12 deletions main/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

static EventGroupHandle_t hdl_evg;
static const char *TAG = "WILLOW/NETWORK";
uint8_t mac_address[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};

void cb_sntp(struct timeval *tv)
{
Expand All @@ -49,34 +48,30 @@ void cb_sntp(struct timeval *tv)
}
}

void set_hostname(esp_mac_type_t emt)
void set_hostname(esp_netif_t *netif, esp_mac_type_t mac_type)
{
esp_err_t ret = ESP_OK;
uint8_t mac[MAC_ADDR_SIZE];

ret = esp_read_mac(mac, emt);
ret = esp_read_mac(mac, mac_type);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "failed to read MAC address, using default hostname (%s)", CONFIG_LWIP_LOCAL_HOSTNAME);
return;
}

while (esp_netif_get_nr_of_ifs() == 0) {
vTaskDelay(100 / portTICK_PERIOD_MS);
}

char hostname[HOSTNAME_SIZE];
hdl_netif = esp_netif_next_unsafe(NULL);
hdl_netif = netif;

snprintf(hostname, HOSTNAME_SIZE, "willow-%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4],
mac[5]);

ret = esp_netif_set_hostname(hdl_netif, hostname);
ret = esp_netif_set_hostname(netif, hostname);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "failed to set hostname (%s): %s", hostname, esp_err_to_name(ret));
}
}

static esp_err_t init_sntp(void)
esp_err_t init_sntp(void)
{
ESP_LOGI(TAG, "initializing SNTP client");
esp_err_t ret = ESP_OK;
Expand All @@ -100,7 +95,7 @@ static esp_err_t init_sntp(void)
return ret;
}

static esp_err_t start_sntp(void)
esp_err_t start_sntp(void)
{
char *ntp_config = config_get_char("ntp_config", DEFAULT_NTP_CONFIG);
if (strcmp(ntp_config, "DHCP") == 0) {
Expand Down Expand Up @@ -219,7 +214,7 @@ esp_err_t init_wifi(const char *psk, const char *ssid)
strlcpy((char *)cfg_wifi.sta.password, psk, sizeof(cfg_wifi.sta.password));
strlcpy((char *)cfg_wifi.sta.ssid, ssid, sizeof(cfg_wifi.sta.ssid));

set_hostname(ESP_MAC_WIFI_STA);
set_hostname(netif_wifi, ESP_MAC_WIFI_STA);

ret = esp_wifi_set_config(ESP_IF_WIFI_STA, &cfg_wifi);
if (ret != ESP_OK) {
Expand Down
6 changes: 6 additions & 0 deletions main/network.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "esp_mac.h"
#include "esp_netif.h"

extern esp_netif_t *hdl_netif;

esp_err_t init_wifi(const char *psk, const char *ssid);
esp_err_t init_ethernet(void);
esp_err_t init_sntp(void);
esp_err_t start_sntp(void);
void get_mac_address(void);
void set_hostname(esp_netif_t *netif, esp_mac_type_t mac_type);
Loading