feat(v2): ESP32-S3 controller with Ethernet, I2S mic beat detection and ESP-NOW
New ESP-IDF project in v2/ that replaces the Pi and the UART bridge: - W5500 Ethernet, INMP441 beat/tempo detector (from esp32-eth), ESP-NOW master - LittleFS data store and a JSON API over the Pi's db/*.json collections, with /db import and export; web UI minified and gzipped at build time - Zone play/stop as 0x4C GROUP_CMD, preset push/send as CMD (binary v2 envelope, byte-identical to src/util/binary_envelope.py), sequencer and scenes, driver registration from the boot hello - Reliable bulk link: acks with retransmit, fragments, state-version digest and resync; beat frames sent twice with a lead so drivers fire together - Bench tooling: lossy-link mode, synthetic beats with echo measurement, USB and logic-analyzer latency scripts, and docs/BENCH_TEST.md Needs the matching led-driver changes (p2p branch) to receive the link and beat frames. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7336c7fb22
commit
a94d6a7de5
37 files changed
+5109
No files matched your search
+221
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* v2 controller: W5500 Ethernet, INMP441 beat detection, ESP-NOW master.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_eth.h"
|
||||
#include "esp_mac.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_system.h"
|
||||
#include "mic.h"
|
||||
#include "beat.h"
|
||||
#include "espnow_tx.h"
|
||||
#include "store.h"
|
||||
#include "api.h"
|
||||
#include "engine.h"
|
||||
#include "rel.h"
|
||||
#include "web.h"
|
||||
|
||||
static const char *TAG = "eth";
|
||||
|
||||
static httpd_handle_t s_server = NULL;
|
||||
static esp_netif_t *s_eth_netif = NULL;
|
||||
|
||||
static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
{
|
||||
beat_info_t bi;
|
||||
espnow_stats_t es;
|
||||
rel_stats_t rs;
|
||||
beat_get_info(&bi);
|
||||
espnow_tx_stats(&es);
|
||||
rel_stats(&rs);
|
||||
char buf[512];
|
||||
int n = snprintf(buf, sizeof(buf),
|
||||
"{\"uptime_s\":%lld,\"free_heap\":%lu,\"bpm\":%.1f,\"beats\":%lu,"
|
||||
"\"espnow\":{\"beats_sent\":%lu,\"beat_send_fail\":%lu,\"bulk_sent\":%lu,"
|
||||
"\"bulk_retries\":%lu,\"bulk_failed\":%lu,\"sim_dropped\":%lu},"
|
||||
"\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu}}",
|
||||
(long long)(esp_timer_get_time() / 1000000), (unsigned long)esp_get_free_heap_size(),
|
||||
bi.bpm, (unsigned long)bi.beat_count, (unsigned long)es.beats_sent,
|
||||
(unsigned long)es.beat_send_fail, (unsigned long)es.bulk_sent,
|
||||
(unsigned long)es.bulk_retries, (unsigned long)es.bulk_failed, (unsigned long)es.sim_dropped, rel_ver(),
|
||||
(unsigned long)rs.acked, (unsigned long)rs.ack_timeouts, (unsigned long)rs.retransmits,
|
||||
(unsigned long)rs.resyncs, (unsigned long)rs.digests);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_send(req, buf, n);
|
||||
}
|
||||
|
||||
static void start_webserver(void)
|
||||
{
|
||||
if (s_server) {
|
||||
return;
|
||||
}
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.uri_match_fn = httpd_uri_match_wildcard;
|
||||
config.max_uri_handlers = 64;
|
||||
config.stack_size = 8192;
|
||||
config.lru_purge_enable = true;
|
||||
if (httpd_start(&s_server, &config) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "HTTP server start failed");
|
||||
return;
|
||||
}
|
||||
const httpd_uri_t status_uri = { .uri = "/status", .method = HTTP_GET, .handler = status_get_handler };
|
||||
httpd_register_uri_handler(s_server, &status_uri);
|
||||
web_register(s_server);
|
||||
api_register(s_server);
|
||||
ESP_LOGI(TAG, "HTTP server started on port %d", config.server_port);
|
||||
}
|
||||
|
||||
static void eth_event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
|
||||
uint8_t mac[6] = {0};
|
||||
|
||||
switch (event_id) {
|
||||
case ETHERNET_EVENT_CONNECTED: {
|
||||
eth_speed_t speed;
|
||||
eth_duplex_t duplex;
|
||||
esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac);
|
||||
esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &speed);
|
||||
esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &duplex);
|
||||
ESP_LOGI(TAG, "Ethernet Link Up, MAC " MACSTR, MAC2STR(mac));
|
||||
ESP_LOGI(TAG, "%s Mbps %s Duplex",
|
||||
speed == ETH_SPEED_100M ? "100" : "10",
|
||||
duplex == ETH_DUPLEX_FULL ? "Full" : "Half");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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 = &event->ip_info;
|
||||
|
||||
ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&ip->ip));
|
||||
ESP_LOGI(TAG, "Mask: " IPSTR, IP2STR(&ip->netmask));
|
||||
ESP_LOGI(TAG, "GW: " IPSTR, IP2STR(&ip->gw));
|
||||
|
||||
// Server binds to all interfaces, so it keeps working across lease changes
|
||||
start_webserver();
|
||||
}
|
||||
|
||||
static esp_err_t w5500_init(esp_eth_handle_t *eth_handle_out)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
esp_eth_mac_t *mac = NULL;
|
||||
esp_eth_phy_t *phy = NULL;
|
||||
esp_eth_handle_t eth_handle = NULL;
|
||||
|
||||
// INT pin uses a GPIO ISR
|
||||
ESP_RETURN_ON_ERROR(gpio_install_isr_service(0), TAG, "GPIO ISR service install failed");
|
||||
|
||||
spi_bus_config_t buscfg = {
|
||||
.miso_io_num = CONFIG_W5500_MISO_GPIO,
|
||||
.mosi_io_num = CONFIG_W5500_MOSI_GPIO,
|
||||
.sclk_io_num = CONFIG_W5500_SCLK_GPIO,
|
||||
.quadwp_io_num = -1,
|
||||
.quadhd_io_num = -1,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(spi_bus_initialize(CONFIG_W5500_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO),
|
||||
TAG, "SPI bus init failed");
|
||||
|
||||
spi_device_interface_config_t spi_devcfg = {
|
||||
.mode = 0,
|
||||
.clock_speed_hz = CONFIG_W5500_SPI_CLOCK_MHZ * 1000 * 1000,
|
||||
.queue_size = 20,
|
||||
.spics_io_num = CONFIG_W5500_CS_GPIO,
|
||||
};
|
||||
|
||||
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_W5500_SPI_HOST, &spi_devcfg);
|
||||
w5500_config.int_gpio_num = CONFIG_W5500_INT_GPIO;
|
||||
if (CONFIG_W5500_INT_GPIO < 0) {
|
||||
w5500_config.poll_period_ms = CONFIG_W5500_POLL_PERIOD_MS;
|
||||
}
|
||||
|
||||
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
|
||||
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
|
||||
phy_config.reset_gpio_num = CONFIG_W5500_RST_GPIO;
|
||||
|
||||
mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
|
||||
phy = esp_eth_phy_new_w5500(&phy_config);
|
||||
ESP_GOTO_ON_FALSE(mac && phy, ESP_FAIL, err, TAG, "W5500 MAC/PHY create failed");
|
||||
|
||||
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
|
||||
ESP_GOTO_ON_ERROR(esp_eth_driver_install(ð_config, ð_handle), err, TAG,
|
||||
"Driver install failed (check W5500 wiring/power)");
|
||||
|
||||
// The W5500 has no factory MAC; use the ESP32's Ethernet MAC from eFuse
|
||||
uint8_t eth_mac[6];
|
||||
ESP_GOTO_ON_ERROR(esp_read_mac(eth_mac, ESP_MAC_ETH), err, TAG, "Read MAC failed");
|
||||
ESP_GOTO_ON_ERROR(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, eth_mac), err, TAG, "Set MAC failed");
|
||||
|
||||
*eth_handle_out = eth_handle;
|
||||
return ESP_OK;
|
||||
|
||||
err:
|
||||
if (eth_handle) {
|
||||
esp_eth_driver_uninstall(eth_handle);
|
||||
}
|
||||
if (mac) {
|
||||
mac->del(mac);
|
||||
}
|
||||
if (phy) {
|
||||
phy->del(phy);
|
||||
}
|
||||
spi_bus_free(CONFIG_W5500_SPI_HOST);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
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(store_init());
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
esp_eth_handle_t eth_handle = NULL;
|
||||
ESP_ERROR_CHECK(w5500_init(ð_handle));
|
||||
|
||||
esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
|
||||
s_eth_netif = esp_netif_new(&netif_cfg);
|
||||
ESP_ERROR_CHECK(esp_netif_attach(s_eth_netif, esp_eth_new_netif_glue(eth_handle)));
|
||||
|
||||
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));
|
||||
|
||||
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
|
||||
ESP_ERROR_CHECK(espnow_tx_init());
|
||||
ESP_ERROR_CHECK(rel_init());
|
||||
ESP_ERROR_CHECK(engine_init());
|
||||
ESP_ERROR_CHECK(mic_start());
|
||||
}
|
||||
Reference in new issue
Block a user