From 30dcc47ba967839d4ce31d845ec2edd008c389e8 Mon Sep 17 00:00:00 2001 From: Karl Osterseher Date: Sat, 21 Jan 2023 23:25:52 +0100 Subject: [PATCH] - add support for Ethernet using an external PHY currently it is either WiFi or Ethernet. If Ethernet is enabled through menuconfig it gets priority and WiFi is disabled. --- components/eth_interface/CMakeLists.txt | 2 + components/eth_interface/Kconfig.projbuild | 170 ++++++++++++++++ components/eth_interface/component.mk | 1 + components/eth_interface/eth_interface.c | 190 ++++++++++++++++++ .../eth_interface/include/eth_interface.h | 6 + components/lightsnapcast/player.c | 64 +++--- .../ui_http_server/include/ui_http_server.h | 2 +- components/ui_http_server/ui_http_server.c | 24 ++- main/Kconfig.projbuild | 5 +- main/main.c | 21 +- sdkconfig | 35 +++- sdkconfig.old | 41 +++- 12 files changed, 496 insertions(+), 65 deletions(-) create mode 100644 components/eth_interface/CMakeLists.txt create mode 100644 components/eth_interface/Kconfig.projbuild create mode 100644 components/eth_interface/component.mk create mode 100644 components/eth_interface/eth_interface.c create mode 100644 components/eth_interface/include/eth_interface.h diff --git a/components/eth_interface/CMakeLists.txt b/components/eth_interface/CMakeLists.txt new file mode 100644 index 0000000..dc2e9fe --- /dev/null +++ b/components/eth_interface/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "eth_interface.c" + INCLUDE_DIRS "include") diff --git a/components/eth_interface/Kconfig.projbuild b/components/eth_interface/Kconfig.projbuild new file mode 100644 index 0000000..f468908 --- /dev/null +++ b/components/eth_interface/Kconfig.projbuild @@ -0,0 +1,170 @@ +menu "Snapclient Ethernet Configuration" + config SNAPCLIENT_ENABLE_ETHERNET + bool "enable Ethernet" + default n + help + Enable Ethernet interface + + config SNAPCLIENT_USE_SPI_ETHERNET + bool + depends on SNAPCLIENT_ENABLE_ETHERNET + + choice SNAPCLIENT_ETHERNET_TYPE + prompt "Ethernet Type" + default SNAPCLIENT_USE_INTERNAL_ETHERNET if IDF_TARGET_ESP32 + default SNAPCLIENT_USE_W5500 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Select which kind of Ethernet will be used in the example. + + config SNAPCLIENT_USE_INTERNAL_ETHERNET + depends on IDF_TARGET_ESP32 + select ETH_USE_ESP32_EMAC + bool "Internal EMAC" + help + Select internal Ethernet MAC controller. + + config SNAPCLIENT_USE_DM9051 + bool "DM9051 Module" + select SNAPCLIENT_USE_SPI_ETHERNET + select ETH_USE_SPI_ETHERNET + select ETH_SPI_ETHERNET_DM9051 + help + Select external SPI-Ethernet module (DM9051). + + config SNAPCLIENT_USE_W5500 + bool "W5500 Module" + select SNAPCLIENT_USE_SPI_ETHERNET + select ETH_USE_SPI_ETHERNET + select ETH_SPI_ETHERNET_W5500 + help + Select external SPI-Ethernet module (W5500). + endchoice # SNAPCLIENT_ETHERNET_TYPE + + if SNAPCLIENT_USE_INTERNAL_ETHERNET + choice SNAPCLIENT_ETH_PHY_MODEL + prompt "Ethernet PHY Device" + default SNAPCLIENT_ETH_PHY_IP101 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Select the Ethernet PHY device to use in the example. + + config SNAPCLIENT_ETH_PHY_IP101 + bool "IP101" + help + IP101 is a single port 10/100 MII/RMII/TP/Fiber Fast Ethernet Transceiver. + Goto http://www.icplus.com.tw/pp-IP101G.html for more information about it. + + config SNAPCLIENT_ETH_PHY_RTL8201 + bool "RTL8201/SR8201" + help + RTL8201F/SR8201F is a single port 10/100Mb Ethernet Transceiver with auto MDIX. + Goto http://www.corechip-sz.com/productsview.asp?id=22 for more information about it. + + config SNAPCLIENT_ETH_PHY_LAN8720 + bool "LAN8720" + help + LAN8720A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX Support. + Goto https://www.microchip.com/LAN8720A for more information about it. + + config SNAPCLIENT_ETH_PHY_DP83848 + bool "DP83848" + help + DP83848 is a single port 10/100Mb/s Ethernet Physical Layer Transceiver. + Goto http://www.ti.com/product/DP83848J for more information about it. + + config SNAPCLIENT_ETH_PHY_KSZ8041 + bool "KSZ8041" + help + The KSZ8041 is a single supply 10Base-T/100Base-TX Physical Layer Transceiver. + Goto https://www.microchip.com/wwwproducts/en/KSZ8041 for more information about it. + endchoice # SNAPCLIENT_ETH_PHY_MODEL + + config SNAPCLIENT_ETH_MDC_GPIO + int "SMI MDC GPIO number" + default 23 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the GPIO number used by SMI MDC. + + config SNAPCLIENT_ETH_MDIO_GPIO + int "SMI MDIO GPIO number" + default 18 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the GPIO number used by SMI MDIO. + endif # SNAPCLIENT_USE_INTERNAL_ETHERNET + + if SNAPCLIENT_USE_SPI_ETHERNET + config SNAPCLIENT_ETH_SPI_HOST + int "SPI Host Number" + range 0 2 + default 1 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the SPI host used to communicate with the SPI Ethernet Controller. + + config SNAPCLIENT_ETH_SPI_SCLK_GPIO + int "SPI SCLK GPIO number" + range 0 33 + default 20 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the GPIO number used by SPI SCLK. + + config SNAPCLIENT_ETH_SPI_MOSI_GPIO + int "SPI MOSI GPIO number" + range 0 33 + default 19 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the GPIO number used by SPI MOSI. + + config SNAPCLIENT_ETH_SPI_MISO_GPIO + int "SPI MISO GPIO number" + range 0 33 + default 18 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the GPIO number used by SPI MISO. + + config SNAPCLIENT_ETH_SPI_CS_GPIO + int "SPI CS GPIO number" + range 0 33 + default 21 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the GPIO number used by SPI CS. + + config SNAPCLIENT_ETH_SPI_CLOCK_MHZ + int "SPI clock speed (MHz)" + range 5 80 + default 36 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the clock speed (MHz) of SPI interface. + + config SNAPCLIENT_ETH_SPI_INT_GPIO + int "Interrupt GPIO number" + default 4 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the GPIO number used by the SPI Ethernet module interrupt line. + endif # SNAPCLIENT_USE_SPI_ETHERNET + + config SNAPCLIENT_ETH_PHY_RST_GPIO + int "PHY Reset GPIO number" + default 17 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set the GPIO number used to reset PHY chip. + Set to -1 to disable PHY chip hardware reset. + + config SNAPCLIENT_ETH_PHY_ADDR + int "PHY Address" + range 0 31 + default 0 + depends on SNAPCLIENT_ENABLE_ETHERNET + help + Set PHY address according your board schematic. +endmenu diff --git a/components/eth_interface/component.mk b/components/eth_interface/component.mk new file mode 100644 index 0000000..306fb05 --- /dev/null +++ b/components/eth_interface/component.mk @@ -0,0 +1 @@ +COMPONENT_SRCDIRS := . diff --git a/components/eth_interface/eth_interface.c b/components/eth_interface/eth_interface.c new file mode 100644 index 0000000..66d6814 --- /dev/null +++ b/components/eth_interface/eth_interface.c @@ -0,0 +1,190 @@ +/* Ethernet Basic 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 +#include +#include "driver/gpio.h" +#include "esp_eth.h" +#include "esp_event.h" +#include "esp_log.h" +#include "esp_netif.h" +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "freertos/task.h" +#include "sdkconfig.h" +#if CONFIG_ETH_USE_SPI_ETHERNET +#include "driver/spi_master.h" +#endif // CONFIG_ETH_USE_SPI_ETHERNET + +static const char *TAG = "ETH"; + +/* 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 EventGroupHandle_t s_eth_event_group; + +/** Event handler for Ethernet events */ +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"); + xEventGroupSetBits(s_eth_event_group, ETH_FAIL_BIT); + 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 IP_EVENT_ETH_GOT_IP */ +static void got_ip_event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + const esp_netif_ip_info_t *ip_info = &event->ip_info; + + ESP_LOGI(TAG, "Ethernet Got IP Address"); + ESP_LOGI(TAG, "~~~~~~~~~~~"); + ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip)); + ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask)); + ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw)); + ESP_LOGI(TAG, "~~~~~~~~~~~"); + + xEventGroupSetBits(s_eth_event_group, ETH_CONNECTED_BIT); +} + +void eth_init(void) { + // Initialize TCP/IP network interface (should be called only once in + // application) + ESP_ERROR_CHECK(esp_netif_init()); + // Create default event loop that running in background + ESP_ERROR_CHECK(esp_event_loop_create_default()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t *eth_netif = esp_netif_new(&cfg); + // Set default handlers to process TCP/IP stuffs + ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif)); + // Register user defined event handers + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, + ð_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, + &got_ip_event_handler, NULL)); + + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + phy_config.phy_addr = CONFIG_SNAPCLIENT_ETH_PHY_ADDR; + phy_config.reset_gpio_num = CONFIG_SNAPCLIENT_ETH_PHY_RST_GPIO; + + // phy_config.reset_timeout_ms = 500; + // mac_config.sw_reset_timeout_ms = 500; + +#if CONFIG_SNAPCLIENT_USE_INTERNAL_ETHERNET + mac_config.smi_mdc_gpio_num = CONFIG_SNAPCLIENT_ETH_MDC_GPIO; + mac_config.smi_mdio_gpio_num = CONFIG_SNAPCLIENT_ETH_MDIO_GPIO; + esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); +#if CONFIG_SNAPCLIENT_ETH_PHY_IP101 + esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); +#elif CONFIG_SNAPCLIENT_ETH_PHY_RTL8201 + esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config); +#elif CONFIG_SNAPCLIENT_ETH_PHY_LAN8720 + esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config); +#elif CONFIG_SNAPCLIENT_ETH_PHY_DP83848 + esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config); +#elif CONFIG_SNAPCLIENT_ETH_PHY_KSZ8041 + esp_eth_phy_t *phy = esp_eth_phy_new_ksz8041(&phy_config); +#endif +#elif CONFIG_ETH_USE_SPI_ETHERNET + gpio_install_isr_service(0); + spi_device_handle_t spi_handle = NULL; + spi_bus_config_t buscfg = { + .miso_io_num = CONFIG_SNAPCLIENT_ETH_SPI_MISO_GPIO, + .mosi_io_num = CONFIG_SNAPCLIENT_ETH_SPI_MOSI_GPIO, + .sclk_io_num = CONFIG_SNAPCLIENT_ETH_SPI_SCLK_GPIO, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + }; + ESP_ERROR_CHECK( + spi_bus_initialize(CONFIG_SNAPCLIENT_ETH_SPI_HOST, &buscfg, 1)); +#if CONFIG_SNAPCLIENT_USE_DM9051 + spi_device_interface_config_t devcfg = { + .command_bits = 1, + .address_bits = 7, + .mode = 0, + .clock_speed_hz = CONFIG_SNAPCLIENT_ETH_SPI_CLOCK_MHZ * 1000 * 1000, + .spics_io_num = CONFIG_SNAPCLIENT_ETH_SPI_CS_GPIO, + .queue_size = 20}; + ESP_ERROR_CHECK( + spi_bus_add_device(CONFIG_SNAPCLIENT_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); + dm9051_config.int_gpio_num = CONFIG_SNAPCLIENT_ETH_SPI_INT_GPIO; + 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); +#elif CONFIG_SNAPCLIENT_USE_W5500 + spi_device_interface_config_t devcfg = { + .command_bits = 16, // Actually it's the address phase in W5500 SPI frame + .address_bits = 8, // Actually it's the control phase in W5500 SPI frame + .mode = 0, + .clock_speed_hz = CONFIG_SNAPCLIENT_ETH_SPI_CLOCK_MHZ * 1000 * 1000, + .spics_io_num = CONFIG_SNAPCLIENT_ETH_SPI_CS_GPIO, + .queue_size = 20}; + ESP_ERROR_CHECK( + spi_bus_add_device(CONFIG_SNAPCLIENT_ETH_SPI_HOST, &devcfg, &spi_handle)); + /* w5500 ethernet driver is based on spi driver */ + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle); + w5500_config.int_gpio_num = CONFIG_SNAPCLIENT_ETH_SPI_INT_GPIO; + esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); + esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config); +#endif +#endif // CONFIG_ETH_USE_SPI_ETHERNET + esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); + esp_eth_handle_t eth_handle = NULL; + ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle)); +#if !CONFIG_SNAPCLIENT_USE_INTERNAL_ETHERNET + /* The SPI Ethernet module might doesn't 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, ETH_CMD_S_MAC_ADDR, + (uint8_t[]){0x02, 0x00, 0x00, 0x12, 0x34, 0x56})); +#endif + /* 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)); + + /* 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); +} diff --git a/components/eth_interface/include/eth_interface.h b/components/eth_interface/include/eth_interface.h new file mode 100644 index 0000000..943e114 --- /dev/null +++ b/components/eth_interface/include/eth_interface.h @@ -0,0 +1,6 @@ +#ifndef _ETH_INTERFACE_H_ +#define _ETH_INTERFACE_H_ + +void eth_init(void); + +#endif /* _ETH_INTERFACE_H_ */ diff --git a/components/lightsnapcast/player.c b/components/lightsnapcast/player.c index 12ae4f7..74bdad2 100644 --- a/components/lightsnapcast/player.c +++ b/components/lightsnapcast/player.c @@ -886,7 +886,7 @@ int32_t allocate_pcm_chunk_memory(pcm_chunk_message_t **pcmChunk, // TODO: x should probably be dynamically calculated as a fraction of buffer // size if allocation fails we try again every 1ms for max. x ms waiting for // chunks to finish playback - uint32_t x = 200; + uint32_t x = 50; for (int i = 0; i < x; i++) { ret = allocate_pcm_chunk_memory_caps(*pcmChunk, bytes, MALLOC_CAP_32BIT | MALLOC_CAP_EXEC); @@ -1333,9 +1333,8 @@ static void player_task(void *pvParameters) { shortMedian = MEDIANFILTER_Insert(&shortMedianFilter, avg); miniMedian = MEDIANFILTER_Insert(&miniMedianFilter, avg); - // resync if we are getting very late / early. - // hopefully being early will get ok - // through apll speed control + // resync hard if we are getting very late / early. + // rest gets tuned in through apll speed control if ((uxQueueMessagesWaiting(pcmChkQHdl) == 0) || ((abs(avg) > hardResyncThreshold) && MEDIANFILTER_isFull(&shortMedianFilter))) { @@ -1344,27 +1343,9 @@ static void player_task(void *pvParameters) { chnk = NULL; } - // get count of chunks we are late for - uint32_t c = ceil((float)age / (float)chkDur_us); // round up - // now clear all those chunks which are probably late too - while (c--) { - ret = xQueueReceive(pcmChkQHdl, &chnk, pdMS_TO_TICKS(1)); - if (ret == pdPASS) { - free_pcm_chunk(chnk); - chnk = NULL; - } else { - break; - } - } - wifi_ap_record_t ap; esp_wifi_sta_get_ap_info(&ap); - timer_pause(TIMER_GROUP_1, TIMER_1); - timer_set_auto_reload(TIMER_GROUP_1, TIMER_1, TIMER_AUTORELOAD_DIS); - - i2s_custom_stop(I2S_NUM_0); - ESP_LOGW(TAG, "RESYNCING HARD 2: age %lldus, latency %lldus, free " "%d, largest block %d, %d, rssi: %d", @@ -1372,6 +1353,25 @@ static void player_task(void *pvParameters) { heap_caps_get_largest_free_block(MALLOC_CAP_32BIT), uxQueueMessagesWaiting(pcmChkQHdl), ap.rssi); + // // get count of chunks we are late for + // uint32_t c = ceil((float)age / (float)chkDur_us); // + // round up + // // now clear all those chunks which are probably late too + // while (c--) { + // ret = xQueueReceive(pcmChkQHdl, &chnk, + // pdMS_TO_TICKS(1)); if (ret == pdPASS) { + // free_pcm_chunk(chnk); + // chnk = NULL; + // } else { + // break; + // } + // } + + timer_pause(TIMER_GROUP_1, TIMER_1); + timer_set_auto_reload(TIMER_GROUP_1, TIMER_1, TIMER_AUTORELOAD_DIS); + + i2s_custom_stop(I2S_NUM_0); + initialSync = 0; audio_set_mute(true); @@ -1406,16 +1406,16 @@ static void player_task(void *pvParameters) { // ESP_LOGI (TAG, "%d, %lldus, %lldus %llds, %lld.%lldms", // dir, age, avg, sec, msec, usec); - // ESP_LOGI(TAG, "%d, %lldus, %lldus, %lldus, q:%d", dir, avg, - // shortMedian, miniMedian, uxQueueMessagesWaiting(pcmChkQHdl)); - // ESP_LOGI( TAG, "8b f %d b %d", - // heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL), - // heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | - // MALLOC_CAP_INTERNAL)); - // ESP_LOGI( TAG, "32b f %d b %d", - // heap_caps_get_free_size(MALLOC_CAP_32BIT | - // MALLOC_CAP_EXEC), heap_caps_get_largest_free_block - // (MALLOC_CAP_32BIT | MALLOC_CAP_EXEC)); + // ESP_LOGI(TAG, "%d, %lldus, %lldus, %lldus, q:%d", dir, + // avg, shortMedian, miniMedian, + // uxQueueMessagesWaiting(pcmChkQHdl)); ESP_LOGI( TAG, "8b f + // %d b %d", heap_caps_get_free_size(MALLOC_CAP_8BIT | + // MALLOC_CAP_INTERNAL), + // heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | + // MALLOC_CAP_INTERNAL)); ESP_LOGI( TAG, "32b f %d b %d", + // heap_caps_get_free_size(MALLOC_CAP_32BIT | + // MALLOC_CAP_EXEC), heap_caps_get_largest_free_block + // (MALLOC_CAP_32BIT | MALLOC_CAP_EXEC)); } dir = 0; diff --git a/components/ui_http_server/include/ui_http_server.h b/components/ui_http_server/include/ui_http_server.h index 091be1d..a45ef95 100644 --- a/components/ui_http_server/include/ui_http_server.h +++ b/components/ui_http_server/include/ui_http_server.h @@ -1,7 +1,7 @@ #ifndef __UI_HTTP_SERVER_H__ #define __UI_HTTP_SERVER_H__ -void init_http_server_task(void); +void init_http_server_task(char *key); typedef struct { char str_value[4]; diff --git a/components/ui_http_server/ui_http_server.c b/components/ui_http_server/ui_http_server.c index cfe2aaa..d100556 100644 --- a/components/ui_http_server/ui_http_server.c +++ b/components/ui_http_server/ui_http_server.c @@ -33,6 +33,8 @@ static const char *TAG = "HTTP"; static QueueHandle_t xQueueHttp; +static esp_netif_t *netInterface = NULL; + /** * */ @@ -323,8 +325,8 @@ esp_err_t start_server(const char *base_path, int port) { static void http_server_task(void *pvParameters) { /* Get the local IP address */ esp_netif_ip_info_t ip_info; - ESP_ERROR_CHECK(esp_netif_get_ip_info( - esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info)); + + ESP_ERROR_CHECK(esp_netif_get_ip_info(netInterface, &ip_info)); char ipString[64]; sprintf(ipString, IPSTR, IP2STR(&ip_info.ip)); @@ -402,14 +404,24 @@ static void http_server_task(void *pvParameters) { /** * */ -void init_http_server_task(void) { +void init_http_server_task(char *key) { + if (!key) { + ESP_LOGE(TAG, + "key should be \"WIFI_STA_DEF\", \"WIFI_AP_DEF\" or \"ETH_DEF\""); + return; + } + + netInterface = esp_netif_get_handle_from_ifkey(key); + if (!netInterface) { + ESP_LOGE(TAG, "can't get net interface for %s", key); + return; + } + // Initialize SPIFFS ESP_LOGI(TAG, "Initializing SPIFFS"); if (SPIFFS_Mount("/html", "storage", 6) != ESP_OK) { ESP_LOGE(TAG, "SPIFFS mount failed"); - while (1) { - vTaskDelay(1); - } + return; } // Create Queue diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 6d925db..abf4fbb 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -1,5 +1,4 @@ -menu "Snapcast Configuration" - +menu "Snapclient Configuration" config SNAPSERVER_USE_MDNS bool "Use mDNS" default true @@ -27,13 +26,11 @@ menu "Snapcast Configuration" Name of the client to register the snapserver. menu "HTTP Server Setting" - config WEB_PORT int "User interface HTTP Server Port" default 8000 help HTTP server port to use. - endmenu endmenu diff --git a/main/main.c b/main/main.c index b32f3d2..9b52237 100644 --- a/main/main.c +++ b/main/main.c @@ -12,6 +12,7 @@ #include "esp_log.h" #include "esp_system.h" #include "esp_wifi.h" +#include "eth_interface.h" #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "freertos/task.h" @@ -2670,7 +2671,15 @@ void app_main(void) { esp_log_level_set("HEADPHONE", ESP_LOG_NONE); esp_log_level_set("gpio", ESP_LOG_NONE); - esp_timer_init(); +#if CONFIG_SNAPCLIENT_ENABLE_ETHERNET + // ethernet pcb reset pin + gpio_config_t cfg = {.pin_bit_mask = BIT64(GPIO_NUM_17), + .mode = GPIO_MODE_DEF_INPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_ENABLE, + .intr_type = GPIO_INTR_DISABLE}; + gpio_config(&cfg); +#endif // some codecs need i2s mclk for initialization i2s_config_t i2s_config0 = { @@ -2707,13 +2716,19 @@ void app_main(void) { init_player(); // setup_ma120(); +#if CONFIG_SNAPCLIENT_ENABLE_ETHERNET + eth_init(); + // pass "WIFI_STA_DEF", "WIFI_AP_DEF", "ETH_DEF" + init_http_server_task("ETH_DEF"); +#else // Enable and setup WIFI in station mode and connect to Access point setup in // menu config or set up provisioning mode settable in menuconfig wifi_init(); ESP_LOGI(TAG, "Connected to AP"); - // http server for control operations and user interface - init_http_server_task(); + // pass "WIFI_STA_DEF", "WIFI_AP_DEF", "ETH_DEF" + init_http_server_task("WIFI_STA_DEF"); +#endif // Enable websocket server // ESP_LOGI(TAG, "Setup ws server"); diff --git a/sdkconfig b/sdkconfig index b8c11b5..9314ce1 100644 --- a/sdkconfig +++ b/sdkconfig @@ -187,6 +187,24 @@ CONFIG_USE_BIQUAD_ASM=y # CONFIG_SNAPCLIENT_USE_SOFT_VOL is not set # end of ESP32 DSP processor config +# +# Snapclient Ethernet Configuration +# +CONFIG_SNAPCLIENT_ENABLE_ETHERNET=y +CONFIG_SNAPCLIENT_USE_INTERNAL_ETHERNET=y +# CONFIG_SNAPCLIENT_USE_DM9051 is not set +# CONFIG_SNAPCLIENT_USE_W5500 is not set +# CONFIG_SNAPCLIENT_ETH_PHY_IP101 is not set +# CONFIG_SNAPCLIENT_ETH_PHY_RTL8201 is not set +CONFIG_SNAPCLIENT_ETH_PHY_LAN8720=y +# CONFIG_SNAPCLIENT_ETH_PHY_DP83848 is not set +# CONFIG_SNAPCLIENT_ETH_PHY_KSZ8041 is not set +CONFIG_SNAPCLIENT_ETH_MDC_GPIO=23 +CONFIG_SNAPCLIENT_ETH_MDIO_GPIO=18 +CONFIG_SNAPCLIENT_ETH_PHY_RST_GPIO=17 +CONFIG_SNAPCLIENT_ETH_PHY_ADDR=0 +# end of Snapclient Ethernet Configuration + # # SNTP Configuration # @@ -202,7 +220,7 @@ CONFIG_WIFI_MAXIMUM_RETRY=0 # end of Wifi Configuration # -# Snapcast Configuration +# Snapclient Configuration # CONFIG_SNAPSERVER_USE_MDNS=y CONFIG_SNAPCLIENT_NAME="esp-snapclient" @@ -212,7 +230,7 @@ CONFIG_SNAPCLIENT_NAME="esp-snapclient" # CONFIG_WEB_PORT=8000 # end of HTTP Server Setting -# end of Snapcast Configuration +# end of Snapclient Configuration # # Compiler options @@ -472,9 +490,10 @@ CONFIG_ETH_PHY_INTERFACE_RMII=y CONFIG_ETH_RMII_CLK_INPUT=y # CONFIG_ETH_RMII_CLK_OUTPUT is not set CONFIG_ETH_RMII_CLK_IN_GPIO=0 -CONFIG_ETH_DMA_BUFFER_SIZE=512 -CONFIG_ETH_DMA_RX_BUFFER_NUM=10 -CONFIG_ETH_DMA_TX_BUFFER_NUM=10 +CONFIG_ETH_DMA_BUFFER_SIZE=1024 +CONFIG_ETH_DMA_RX_BUFFER_NUM=30 +CONFIG_ETH_DMA_TX_BUFFER_NUM=5 +# CONFIG_ETH_SOFT_FLOW_CONTROL is not set CONFIG_ETH_USE_SPI_ETHERNET=y # CONFIG_ETH_SPI_ETHERNET_DM9051 is not set # CONFIG_ETH_SPI_ETHERNET_W5500 is not set @@ -587,11 +606,11 @@ CONFIG_ESP32_WIFI_MGMT_SBUF_NUM=32 # CONFIG_WIFI_LOG_DEFAULT_LEVEL_NONE is not set # CONFIG_WIFI_LOG_DEFAULT_LEVEL_ERROR is not set # CONFIG_WIFI_LOG_DEFAULT_LEVEL_WARN is not set -CONFIG_WIFI_LOG_DEFAULT_LEVEL_INFO=y +# CONFIG_WIFI_LOG_DEFAULT_LEVEL_INFO is not set # CONFIG_WIFI_LOG_DEFAULT_LEVEL_DEBUG is not set -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_VERBOSE is not set +CONFIG_WIFI_LOG_DEFAULT_LEVEL_VERBOSE=y # CONFIG_ESP32_WIFI_IRAM_OPT is not set -# CONFIG_ESP32_WIFI_RX_IRAM_OPT is not set +CONFIG_ESP32_WIFI_RX_IRAM_OPT=y CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE=y # CONFIG_ESP_WIFI_SLP_IRAM_OPT is not set # CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE is not set diff --git a/sdkconfig.old b/sdkconfig.old index cbceaca..dc64063 100644 --- a/sdkconfig.old +++ b/sdkconfig.old @@ -187,6 +187,24 @@ CONFIG_USE_BIQUAD_ASM=y # CONFIG_SNAPCLIENT_USE_SOFT_VOL is not set # end of ESP32 DSP processor config +# +# Snapclient Ethernet Configuration +# +CONFIG_SNAPCLIENT_ENABLE_ETHERNET=y +CONFIG_SNAPCLIENT_USE_INTERNAL_ETHERNET=y +# CONFIG_SNAPCLIENT_USE_DM9051 is not set +# CONFIG_SNAPCLIENT_USE_W5500 is not set +# CONFIG_SNAPCLIENT_ETH_PHY_IP101 is not set +# CONFIG_SNAPCLIENT_ETH_PHY_RTL8201 is not set +CONFIG_SNAPCLIENT_ETH_PHY_LAN8720=y +# CONFIG_SNAPCLIENT_ETH_PHY_DP83848 is not set +# CONFIG_SNAPCLIENT_ETH_PHY_KSZ8041 is not set +CONFIG_SNAPCLIENT_ETH_MDC_GPIO=23 +CONFIG_SNAPCLIENT_ETH_MDIO_GPIO=18 +CONFIG_SNAPCLIENT_ETH_PHY_RST_GPIO=17 +CONFIG_SNAPCLIENT_ETH_PHY_ADDR=0 +# end of Snapclient Ethernet Configuration + # # SNTP Configuration # @@ -202,7 +220,7 @@ CONFIG_WIFI_MAXIMUM_RETRY=0 # end of Wifi Configuration # -# Snapcast Configuration +# Snapclient Configuration # CONFIG_SNAPSERVER_USE_MDNS=y CONFIG_SNAPCLIENT_NAME="esp-snapclient" @@ -212,7 +230,7 @@ CONFIG_SNAPCLIENT_NAME="esp-snapclient" # CONFIG_WEB_PORT=8000 # end of HTTP Server Setting -# end of Snapcast Configuration +# end of Snapclient Configuration # # Compiler options @@ -472,9 +490,10 @@ CONFIG_ETH_PHY_INTERFACE_RMII=y CONFIG_ETH_RMII_CLK_INPUT=y # CONFIG_ETH_RMII_CLK_OUTPUT is not set CONFIG_ETH_RMII_CLK_IN_GPIO=0 -CONFIG_ETH_DMA_BUFFER_SIZE=512 -CONFIG_ETH_DMA_RX_BUFFER_NUM=10 -CONFIG_ETH_DMA_TX_BUFFER_NUM=10 +CONFIG_ETH_DMA_BUFFER_SIZE=1024 +CONFIG_ETH_DMA_RX_BUFFER_NUM=30 +CONFIG_ETH_DMA_TX_BUFFER_NUM=5 +# CONFIG_ETH_SOFT_FLOW_CONTROL is not set CONFIG_ETH_USE_SPI_ETHERNET=y # CONFIG_ETH_SPI_ETHERNET_DM9051 is not set # CONFIG_ETH_SPI_ETHERNET_W5500 is not set @@ -587,9 +606,9 @@ CONFIG_ESP32_WIFI_MGMT_SBUF_NUM=32 # CONFIG_WIFI_LOG_DEFAULT_LEVEL_NONE is not set # CONFIG_WIFI_LOG_DEFAULT_LEVEL_ERROR is not set # CONFIG_WIFI_LOG_DEFAULT_LEVEL_WARN is not set -CONFIG_WIFI_LOG_DEFAULT_LEVEL_INFO=y +# CONFIG_WIFI_LOG_DEFAULT_LEVEL_INFO is not set # CONFIG_WIFI_LOG_DEFAULT_LEVEL_DEBUG is not set -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_VERBOSE is not set +CONFIG_WIFI_LOG_DEFAULT_LEVEL_VERBOSE=y # CONFIG_ESP32_WIFI_IRAM_OPT is not set CONFIG_ESP32_WIFI_RX_IRAM_OPT=y CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE=y @@ -809,8 +828,8 @@ CONFIG_LWIP_TCP_MSS=1460 CONFIG_LWIP_TCP_TMR_INTERVAL=250 CONFIG_LWIP_TCP_MSL=60000 CONFIG_LWIP_TCP_SND_BUF_DEFAULT=11680 -CONFIG_LWIP_TCP_WND_DEFAULT=11680 -CONFIG_LWIP_TCP_RECVMBOX_SIZE=10 +CONFIG_LWIP_TCP_WND_DEFAULT=42340 +CONFIG_LWIP_TCP_RECVMBOX_SIZE=31 CONFIG_LWIP_TCP_QUEUE_OOSEQ=y CONFIG_LWIP_TCP_SACK_OUT=y # CONFIG_LWIP_TCP_KEEP_CONNECTION_WHEN_IP_CHANGES is not set @@ -1369,8 +1388,8 @@ CONFIG_TCP_SYNMAXRTX=12 CONFIG_TCP_MSS=1460 CONFIG_TCP_MSL=60000 CONFIG_TCP_SND_BUF_DEFAULT=11680 -CONFIG_TCP_WND_DEFAULT=11680 -CONFIG_TCP_RECVMBOX_SIZE=10 +CONFIG_TCP_WND_DEFAULT=42340 +CONFIG_TCP_RECVMBOX_SIZE=31 CONFIG_TCP_QUEUE_OOSEQ=y # CONFIG_ESP_TCP_KEEP_CONNECTION_WHEN_IP_CHANGES is not set CONFIG_TCP_OVERSIZE_MSS=y