P4-Nano: controller port with the C6 as ESP-NOW radio, and the simulator
The controller now builds for the Waveshare ESP32-P4-Nano only (plus the linux sim and the desktop); the S3 stays for the USB/network bridge in bridge/. - radio-c6/: C6 firmware, esp-hosted plus ESP-NOW over peer data. The P4 loads it from the slave_fw partition when the C6 runs another version (c6_update.c) - bridge_hosted.c: the C6 as the always-on "c6" bridge, and the P4 as a network bridge on UDP 8766 for the desktop - Ethernet on the P4's EMAC, ES8311 mic; default address 192.168.50.20. The W5500, INMP441 and direct ESP-NOW code is gone - The simulated rig at /sim/ is built into the board image, with an always-on "browser" bridge. Its frames go through a queue so a slow page cannot hold up the radio, and frames for simulated bars are not sent over the C6. Receiver buffers are on the heap; a bar's flash file is only written when it changes - HTTP server: more handlers, sockets and stack for the rig - docs/P4_NANO.md, README and Makefile updated; graphify output Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ba8e160aee
commit
4ab0c7c6d7
101 files changed
+3284
-971
No files matched your search
+23
-6
@@ -7,12 +7,19 @@ if(IDF_TARGET STREQUAL "linux")
|
||||
REQUIRES esp_http_server esp_timer esp_event cjson
|
||||
PRIV_REQUIRES mbedtls lwip esp_app_format)
|
||||
else()
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c"
|
||||
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "dmx_wire.c" "ws.c" "ws_client.c" "wifi_drv.c" "res_dmx.c" "auth.c"
|
||||
INCLUDE_DIRS "." "../bridge/main"
|
||||
REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash app_update esp_app_format
|
||||
esp_http_server esp_timer esp_driver_i2s esp_wifi esp_rom cjson esp_driver_gpio
|
||||
PRIV_REQUIRES littlefs mdns mbedtls lwip w5500)
|
||||
# Waveshare ESP32-P4-Nano (docs/P4_NANO.md): internal EMAC Ethernet, ES8311 mic, and the C6 next to it for
|
||||
# Wi-Fi (esp-hosted) and ESP-NOW (bridge_hosted.c talking to radio-c6/)
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c"
|
||||
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "dmx_wire.c" "ws.c" "ws_client.c" "wifi_drv.c" "res_dmx.c" "auth.c"
|
||||
"bridge_hosted.c" "c6_update.c"
|
||||
# the simulated rig at /sim/, shared with the host builds
|
||||
"../sim-controller/components/sim/sim_rig.c" "../sim-controller/components/sim/sim_net.c"
|
||||
INCLUDE_DIRS "." "../bridge/main"
|
||||
PRIV_INCLUDE_DIRS "../sim-controller/components/sim"
|
||||
REQUIRES esp_eth esp_netif esp_event esp_driver_gpio nvs_flash app_update esp_app_format
|
||||
esp_http_server esp_timer esp_driver_i2s esp_driver_i2c esp_wifi esp_rom cjson
|
||||
esp_hosted esp_wifi_remote esp_codec_dev esp_partition bootloader_support
|
||||
PRIV_REQUIRES littlefs mdns mbedtls lwip)
|
||||
endif()
|
||||
|
||||
# The gzipped web UI is embedded in the app image (run tools/build_web.sh first)
|
||||
@@ -20,3 +27,13 @@ if(NOT EXISTS ${COMPONENT_DIR}/../www_dist/www.pack)
|
||||
message(FATAL_ERROR "www_dist/www.pack is missing: run tools/build_web.sh (make build does)")
|
||||
endif()
|
||||
target_add_binary_data(${COMPONENT_LIB} ${COMPONENT_DIR}/../www_dist/www.pack BINARY)
|
||||
|
||||
# The simulated rig's page and drivers (tools/build_sim_web.sh), served at /sim/. The host build embeds its own
|
||||
# copy (the sim component). Without it the firmware still builds, and /sim/ says how to build the rig.
|
||||
if(NOT IDF_TARGET STREQUAL "linux")
|
||||
set(SIM_PACK ${COMPONENT_DIR}/../www_dist/sim.pack)
|
||||
if(NOT EXISTS ${SIM_PACK})
|
||||
file(WRITE ${SIM_PACK} "WWW1")
|
||||
endif()
|
||||
target_add_binary_data(${COMPONENT_LIB} ${SIM_PACK} BINARY)
|
||||
endif()
|
||||
+69
-62
@@ -1,65 +1,11 @@
|
||||
menu "W5500 config"
|
||||
|
||||
config W5500_SPI_HOST
|
||||
int "SPI host number (1=SPI2, 2=SPI3)"
|
||||
range 1 2
|
||||
default 1
|
||||
|
||||
config W5500_SPI_CLOCK_MHZ
|
||||
int "SPI clock speed (MHz)"
|
||||
range 5 80
|
||||
default 10
|
||||
|
||||
config W5500_SCLK_GPIO
|
||||
int "SCLK GPIO (XIAO D8)"
|
||||
default 7
|
||||
|
||||
config W5500_MISO_GPIO
|
||||
int "MISO GPIO (XIAO D9)"
|
||||
default 8
|
||||
|
||||
config W5500_MOSI_GPIO
|
||||
int "MOSI GPIO (XIAO D10)"
|
||||
default 9
|
||||
|
||||
config W5500_CS_GPIO
|
||||
int "CS GPIO (XIAO D1)"
|
||||
default 2
|
||||
|
||||
config W5500_INT_GPIO
|
||||
int "INT GPIO (XIAO D2), -1 to poll"
|
||||
default 3
|
||||
|
||||
config W5500_POLL_PERIOD_MS
|
||||
int "RX poll period (ms) when INT is -1"
|
||||
default 10
|
||||
|
||||
config W5500_RST_GPIO
|
||||
int "RST GPIO (XIAO D0), -1 if not wired"
|
||||
default 1
|
||||
|
||||
endmenu
|
||||
|
||||
menu "INMP441 mic config"
|
||||
|
||||
config MIC_SCK_GPIO
|
||||
int "SCK GPIO (XIAO D4)"
|
||||
default 5
|
||||
|
||||
config MIC_WS_GPIO
|
||||
int "WS GPIO (XIAO D5)"
|
||||
default 6
|
||||
|
||||
config MIC_SD_GPIO
|
||||
int "SD GPIO (XIAO D6)"
|
||||
default 43
|
||||
menu "Mic config"
|
||||
|
||||
config MIC_STREAM_GAIN
|
||||
int "Digital gain for the audio stream (x)"
|
||||
range 1 256
|
||||
default 16
|
||||
help
|
||||
The INMP441 is quiet at full scale; x16 (+24 dB) makes speech
|
||||
The mic is quiet at full scale; x16 (+24 dB) makes speech
|
||||
audible without clipping at normal distances.
|
||||
|
||||
endmenu
|
||||
@@ -67,8 +13,8 @@ endmenu
|
||||
menu "Beat detection config"
|
||||
|
||||
config BEAT_LED_GPIO
|
||||
int "Beat LED GPIO (XIAO user LED is 21), -1 for none"
|
||||
default 21
|
||||
int "Beat LED GPIO, -1 for none (the P4-Nano has no user LED)"
|
||||
default -1
|
||||
|
||||
config BEAT_LED_ACTIVE_LOW
|
||||
bool "Beat LED lights when the pin is low"
|
||||
@@ -89,7 +35,7 @@ menu "Beat detection config"
|
||||
range -100 -20
|
||||
default -75
|
||||
help
|
||||
The INMP441 self-noise is about -87 dBFS. Background music in a
|
||||
An INMP441's self-noise is about -87 dBFS. Background music in a
|
||||
room can sit around -65 dBFS, so keep this well below that.
|
||||
|
||||
config BEAT_LEAD_MS
|
||||
@@ -106,8 +52,8 @@ endmenu
|
||||
menu "AP config"
|
||||
|
||||
config AP_BUTTON_GPIO
|
||||
int "AP button GPIO (XIAO D3), -1 for none"
|
||||
default 4
|
||||
int "AP button GPIO, -1 for none (the P4-Nano has no user button)"
|
||||
default -1
|
||||
help
|
||||
Active-low push button to ground (internal pull-up). Held while the controller boots, it turns
|
||||
the Wi-Fi access point on even when the ap_enabled setting is off.
|
||||
@@ -141,7 +87,7 @@ menu "ESP-NOW config"
|
||||
|
||||
config BEAT_TEST_GPIO
|
||||
int "GPIO toggled on every beat sent (-1 = off)"
|
||||
range -1 48
|
||||
range -1 54
|
||||
default -1
|
||||
help
|
||||
For latency measurement: a logic analyzer or scope on this pin and on
|
||||
@@ -153,3 +99,64 @@ menu "ESP-NOW config"
|
||||
default 3000
|
||||
|
||||
endmenu
|
||||
|
||||
menu "ESP32-P4-Nano board"
|
||||
depends on IDF_TARGET_ESP32P4
|
||||
|
||||
config P4_DEFAULT_IP_A
|
||||
int "Address without DHCP: first byte"
|
||||
range 0 255
|
||||
default 192
|
||||
help
|
||||
The Nano usually sits on a cable straight to a PC (192.168.50.10), with no DHCP server. After a short
|
||||
DHCP wait it takes this address, /24, no gateway. Settings "static_ips" still come first.
|
||||
config P4_DEFAULT_IP_B
|
||||
int "Address without DHCP: second byte"
|
||||
range 0 255
|
||||
default 168
|
||||
config P4_DEFAULT_IP_C
|
||||
int "Address without DHCP: third byte"
|
||||
range 0 255
|
||||
default 50
|
||||
config P4_DEFAULT_IP_D
|
||||
int "Address without DHCP: fourth byte"
|
||||
range 1 254
|
||||
default 20
|
||||
|
||||
config P4_ETH_MDC_GPIO
|
||||
int "Ethernet MDC GPIO"
|
||||
default 31
|
||||
config P4_ETH_MDIO_GPIO
|
||||
int "Ethernet MDIO GPIO"
|
||||
default 52
|
||||
config P4_ETH_PHY_RST_GPIO
|
||||
int "IP101 PHY reset GPIO (-1: none)"
|
||||
range -1 54
|
||||
default 51
|
||||
|
||||
config P4_CODEC_I2C_SDA_GPIO
|
||||
int "ES8311 I2C SDA GPIO"
|
||||
default 7
|
||||
config P4_CODEC_I2C_SCL_GPIO
|
||||
int "ES8311 I2C SCL GPIO"
|
||||
default 8
|
||||
config P4_CODEC_I2C_ADDR
|
||||
hex "ES8311 I2C address (7-bit)"
|
||||
default 0x18
|
||||
config P4_I2S_MCLK_GPIO
|
||||
int "ES8311 MCLK GPIO"
|
||||
default 13
|
||||
config P4_I2S_BCLK_GPIO
|
||||
int "ES8311 BCLK (SCLK) GPIO"
|
||||
default 12
|
||||
config P4_I2S_WS_GPIO
|
||||
int "ES8311 WS (LRCK) GPIO"
|
||||
default 10
|
||||
config P4_I2S_DIN_GPIO
|
||||
int "ES8311 ADC data into the P4 (ASDOUT) GPIO"
|
||||
default 11
|
||||
config P4_MIC_PGA_DB
|
||||
int "ES8311 microphone gain (dB, 0-30 in 3 dB steps)"
|
||||
range 0 30
|
||||
default 24
|
||||
endmenu
|
||||
+64
-19
@@ -12,6 +12,9 @@
|
||||
#include "lwip/sockets.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "bridge_proto.h"
|
||||
#if CONFIG_IDF_TARGET_ESP32P4
|
||||
#include "bridge_hosted.h"
|
||||
#endif
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
#include "bridge_dmx.h"
|
||||
#include "bridge_serial.h"
|
||||
@@ -26,8 +29,9 @@ static const char *TAG = "bridge";
|
||||
typedef struct {
|
||||
bool used;
|
||||
bool serial; // a USB board (bridge_serial), else UDP
|
||||
bool browser; // the rig page of the host build (sim_rig.c), over its WebSocket
|
||||
bool browser; // the rig page (sim_rig.c), over its WebSocket
|
||||
bool dmx; // a DMX bridge "dmx:<id>" (bridge_dmx.c): its fixtures are drivers of their own
|
||||
bool c6; // the ESP32-P4-Nano's own C6 radio (bridge_hosted.c), always there on that target
|
||||
bridge_dmx_t *dmx_b;
|
||||
char addr[BRIDGE_ADDR_MAX]; // as the settings store it
|
||||
bridge_serial_t *board; // serial: the board (linux)
|
||||
@@ -78,11 +82,21 @@ static bool is_dmx_addr(const char *addr)
|
||||
return strncmp(addr, "dmx:", 4) == 0;
|
||||
}
|
||||
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
static bool is_c6_addr(const char *addr)
|
||||
{
|
||||
return strcmp(addr, "c6") == 0;
|
||||
}
|
||||
|
||||
// The rig page's link (sim-controller/components/sim/sim_rig.c): dst_mac(6) + packet to the page, if it is open
|
||||
bool sim_rig_tx(const uint8_t *frame, size_t len);
|
||||
bool sim_rig_page_open(void);
|
||||
#endif
|
||||
|
||||
// The MACs the rig gives its bars (MAC_PREFIX in sim_rig.c): no real board has one
|
||||
static bool is_sim_mac(const uint8_t *mac)
|
||||
{
|
||||
static const uint8_t PREFIX[3] = { 0x02, 0x51, 0x4D };
|
||||
return memcmp(mac, PREFIX, sizeof(PREFIX)) == 0;
|
||||
}
|
||||
|
||||
static int64_t now_ms(void)
|
||||
{
|
||||
@@ -301,7 +315,19 @@ static esp_err_t activate(const char *addr)
|
||||
link_t *l = &s_links[slot];
|
||||
l->browser = false;
|
||||
l->dmx = false;
|
||||
if (is_dmx_addr(addr)) {
|
||||
l->c6 = false;
|
||||
if (is_c6_addr(addr)) {
|
||||
#if CONFIG_IDF_TARGET_ESP32P4
|
||||
bridge_hosted_set_rx(deliver);
|
||||
l->c6 = true;
|
||||
l->serial = false;
|
||||
l->sock = -1;
|
||||
l->ctl = -1;
|
||||
ESP_LOGI(TAG, "ESP-NOW frames go to the C6 radio");
|
||||
#else
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#endif
|
||||
} else if (is_dmx_addr(addr)) {
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
if (!addr[4]) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
@@ -319,15 +345,11 @@ static esp_err_t activate(const char *addr)
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#endif
|
||||
} else if (is_browser_addr(addr)) {
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
l->browser = true;
|
||||
l->serial = false;
|
||||
l->sock = -1;
|
||||
l->ctl = -1;
|
||||
ESP_LOGW(TAG, "ESP-NOW frames go to the rig page (/sim/), not the radio");
|
||||
#else
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#endif
|
||||
ESP_LOGI(TAG, "ESP-NOW frames go to the rig page (/sim/)");
|
||||
} else if (is_serial_addr(addr)) {
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
const char *dev = strncmp(addr, "serial:", 7) == 0 ? addr + 7 : addr; // "usb:<mac>" goes as it is
|
||||
@@ -399,6 +421,7 @@ static void deactivate(int i)
|
||||
if (s_mic_slot == i) {
|
||||
s_mic_slot = -1;
|
||||
}
|
||||
l->c6 = false;
|
||||
l->used = false;
|
||||
}
|
||||
|
||||
@@ -421,11 +444,24 @@ esp_err_t bridge_set_all(const char addrs[][BRIDGE_ADDR_MAX], size_t n, bridge_r
|
||||
}
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
s_cb = cb;
|
||||
#if CONFIG_IDF_TARGET_ESP32P4
|
||||
// The board's own radio is always one of them, whatever the saved list says
|
||||
if (find_link(BRIDGE_HOSTED_ADDR) < 0) {
|
||||
activate(BRIDGE_HOSTED_ADDR);
|
||||
}
|
||||
// So is the rig page (/sim/), which only gets frames while it is open
|
||||
if (find_link(BRIDGE_BROWSER) < 0) {
|
||||
activate(BRIDGE_BROWSER);
|
||||
}
|
||||
#endif
|
||||
for (int i = 0; i < BRIDGE_MAX_LINKS; i++) {
|
||||
if (!s_links[i].used) {
|
||||
continue;
|
||||
}
|
||||
bool wanted = false;
|
||||
bool wanted = s_links[i].c6;
|
||||
#if CONFIG_IDF_TARGET_ESP32P4
|
||||
wanted = wanted || s_links[i].browser;
|
||||
#endif
|
||||
for (size_t j = 0; j < n; j++) {
|
||||
wanted = wanted || strcmp(s_links[i].addr, addrs[j]) == 0;
|
||||
}
|
||||
@@ -494,17 +530,22 @@ esp_err_t bridge_send(const uint8_t *dst_mac, const void *data, size_t len)
|
||||
continue;
|
||||
}
|
||||
any = true;
|
||||
if (l->dmx) {
|
||||
if (l->c6) {
|
||||
#if CONFIG_IDF_TARGET_ESP32P4
|
||||
// A simulated bar is not on the air: no airtime for frames nothing will acknowledge
|
||||
if (!is_sim_mac(dst_mac) && bridge_hosted_send(dst_mac, data, len) == ESP_OK) {
|
||||
l->tx++;
|
||||
}
|
||||
#endif
|
||||
} else if (l->dmx) {
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
bridge_dmx_send(l->dmx_b, dst_mac, data, len);
|
||||
l->tx++;
|
||||
#endif
|
||||
} else if (l->browser) {
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
if (sim_rig_tx(buf, 6 + len)) { // no page open: the frame is lost, as on air
|
||||
l->tx++;
|
||||
}
|
||||
#endif
|
||||
} else if (l->serial) {
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
bridge_serial_send(l->board, dst_mac, data, len); // a closed port loses the frame, as on air
|
||||
@@ -533,19 +574,21 @@ size_t bridge_status_json(char *out, size_t n)
|
||||
continue;
|
||||
}
|
||||
char body[400];
|
||||
if (l->dmx) {
|
||||
if (l->c6) {
|
||||
#if CONFIG_IDF_TARGET_ESP32P4
|
||||
bridge_hosted_status(body, sizeof(body));
|
||||
#else
|
||||
snprintf(body, sizeof(body), "{}");
|
||||
#endif
|
||||
} else if (l->dmx) {
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
bridge_dmx_status(l->dmx_b, body, sizeof(body));
|
||||
#else
|
||||
snprintf(body, sizeof(body), "{}");
|
||||
#endif
|
||||
} else if (l->browser) {
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
snprintf(body, sizeof(body), "{\"page\":%s,\"tx\":%lu,\"rx\":%lu}",
|
||||
sim_rig_page_open() ? "true" : "false", (unsigned long)l->tx, (unsigned long)l->rx);
|
||||
#else
|
||||
snprintf(body, sizeof(body), "{}");
|
||||
#endif
|
||||
} else if (l->serial) {
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
bridge_serial_status(l->board, body, sizeof(body));
|
||||
@@ -565,7 +608,7 @@ size_t bridge_status_json(char *out, size_t n)
|
||||
(unsigned long)l->tx, (unsigned long)l->rx, (unsigned long)l->audio);
|
||||
}
|
||||
int w = snprintf(out + used, n - used, "%s{\"addr\":\"%s\",\"kind\":\"%s\",\"status\":%s}",
|
||||
first ? "" : ",", l->addr, l->dmx ? "dmx" : l->browser ? "browser" : (l->serial ? "serial" : "udp"), body);
|
||||
first ? "" : ",", l->addr, l->c6 ? "c6" : l->dmx ? "dmx" : l->browser ? "browser" : (l->serial ? "serial" : "udp"), body);
|
||||
if (w < 0 || (size_t)w >= n - used) {
|
||||
break;
|
||||
}
|
||||
@@ -600,6 +643,8 @@ esp_err_t bridge_board_set_channel(const char *addr, uint8_t channel)
|
||||
esp_err_t err = (i >= 0 && s_links[i].serial) ? bridge_serial_set_channel(s_links[i].board, channel) : ESP_ERR_NOT_FOUND;
|
||||
xSemaphoreGive(s_lock);
|
||||
return err;
|
||||
#elif CONFIG_IDF_TARGET_ESP32P4
|
||||
return is_c6_addr(addr) ? bridge_hosted_set_channel(channel) : ESP_ERR_NOT_FOUND;
|
||||
#else
|
||||
(void)addr;
|
||||
(void)channel;
|
||||
|
||||
+5
-3
@@ -10,8 +10,10 @@
|
||||
// once it answers; the list is saved again then (bridge_set_renamed_cb).
|
||||
// "dmx:<id>" a DMX bridge (bridge_dmx.h, linux only): the "dmx_bridge" record id's fixtures, each a
|
||||
// led-driver of its own, sent as Art-Net, sACN or USB-DMX at 40 Hz.
|
||||
// "browser" the rig page the host build serves at /sim/ (linux target only): the simulated drivers
|
||||
// run in the browser and get the frames over the page's WebSocket (sim_rig.c).
|
||||
// "browser" the rig page at /sim/: the simulated drivers run in the browser and get the frames over the
|
||||
// page's WebSocket (sim_rig.c). Always running on esp32p4, whatever the list says.
|
||||
// "c6" the ESP32-P4-Nano's own C6 radio (bridge_hosted.h, esp32p4 only). Always running on
|
||||
// that target, whatever the list says: the P4 has no ESP-NOW of its own.
|
||||
// While one runs, nothing goes out over the air.
|
||||
|
||||
#include <stdbool.h>
|
||||
@@ -36,7 +38,7 @@ esp_err_t bridge_set_all(const char addrs[][BRIDGE_ADDR_MAX], size_t n, bridge_r
|
||||
// Called when a bridge's address changed by itself (a USB board became "usb:<mac>"): save the list
|
||||
void bridge_set_renamed_cb(void (*cb)(void));
|
||||
bool bridge_active(void);
|
||||
// A frame the rig page's drivers sent: src_mac(6) + packet (linux; sim_rig.c). Dropped unless "browser" runs.
|
||||
// A frame the rig page's drivers sent: src_mac(6) + packet (sim_rig.c). Dropped unless "browser" runs.
|
||||
void bridge_browser_rx(const uint8_t *frame, int len);
|
||||
// The driver with this MAC was heard on the browser bridge since start: a simulated driver of the rig page
|
||||
bool bridge_mac_is_browser(const uint8_t mac[6]);
|
||||
|
||||
@@ -0,0 +1,389 @@
|
||||
// The ESP32-P4-Nano's C6 as the controller's ESP-NOW radio, and the UDP bridge service on top of it.
|
||||
// See bridge_hosted.h and bridge/main/radio_proto.h.
|
||||
#include "sdkconfig.h"
|
||||
#if CONFIG_IDF_TARGET_ESP32P4
|
||||
#include "bridge_hosted.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "eh_host_feat_peer_data.h"
|
||||
#include "esp_hosted.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/task.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "bridge_proto.h"
|
||||
#include "c6_update.h"
|
||||
#include "mic.h"
|
||||
#include "radio_proto.h"
|
||||
|
||||
static const char *TAG = "bridge_c6";
|
||||
|
||||
#define TXQ_LEN 64
|
||||
#define DESKTOP_HOLD_MS 2000 // the desktop owns the radio until it has been quiet this long
|
||||
#define INFO_TIMEOUT_MS 3000
|
||||
#define MIC_UDP_TIMEOUT_MS 2000
|
||||
|
||||
typedef struct {
|
||||
uint8_t mac[6];
|
||||
uint8_t len;
|
||||
uint8_t data[BRIDGE_PKT_MAX];
|
||||
} pkt_t;
|
||||
|
||||
static QueueHandle_t s_txq;
|
||||
static bridge_rx_cb_t s_rx_cb;
|
||||
static bool s_started;
|
||||
|
||||
// The C6's last INFO
|
||||
static SemaphoreHandle_t s_lock;
|
||||
static uint8_t s_info[BRIDGE_INFO_LEN];
|
||||
static int64_t s_info_ms = -1;
|
||||
static uint32_t s_tx_msgs, s_tx_frames, s_rx_frames, s_tx_drop, s_own_dropped;
|
||||
|
||||
// The UDP service: the desktop that last sent a frame, and the one asking for microphone audio
|
||||
static int s_udp = -1, s_ctl = -1;
|
||||
static struct sockaddr_in s_desk;
|
||||
static volatile int64_t s_desk_ms = -1;
|
||||
static struct sockaddr_in s_mic_peer;
|
||||
static volatile int64_t s_mic_ms = -1;
|
||||
static bool s_desk_logged;
|
||||
|
||||
static int64_t now_ms(void)
|
||||
{
|
||||
return esp_timer_get_time() / 1000;
|
||||
}
|
||||
|
||||
static bool desktop_active(void)
|
||||
{
|
||||
int64_t t = s_desk_ms;
|
||||
return t >= 0 && now_ms() - t < DESKTOP_HOLD_MS;
|
||||
}
|
||||
|
||||
/* ---- the C6 ------------------------------------------------------------------------------------- */
|
||||
|
||||
static void on_rx(uint32_t id, const uint8_t *data, size_t len, void *ctx)
|
||||
{
|
||||
(void)id;
|
||||
(void)ctx;
|
||||
bool desk = desktop_active();
|
||||
uint8_t up[6 + BRIDGE_PKT_MAX];
|
||||
size_t off = 0;
|
||||
while (off + RADIO_ENTRY_HDR <= len) {
|
||||
uint8_t n = data[off + 6];
|
||||
if (n == 0 || n > BRIDGE_PKT_MAX || off + RADIO_ENTRY_HDR + n > len) {
|
||||
break;
|
||||
}
|
||||
const uint8_t *mac = data + off;
|
||||
const uint8_t *pkt = data + off + RADIO_ENTRY_HDR;
|
||||
s_rx_frames++;
|
||||
if (s_rx_cb) {
|
||||
s_rx_cb(mac, pkt, n);
|
||||
}
|
||||
if (desk && s_udp >= 0) {
|
||||
memcpy(up, mac, 6);
|
||||
memcpy(up + 6, pkt, n);
|
||||
sendto(s_udp, up, 6 + n, 0, (const struct sockaddr *)&s_desk, sizeof(s_desk));
|
||||
}
|
||||
off += RADIO_ENTRY_HDR + n;
|
||||
}
|
||||
}
|
||||
|
||||
static void on_info(uint32_t id, const uint8_t *data, size_t len, void *ctx)
|
||||
{
|
||||
(void)id;
|
||||
(void)ctx;
|
||||
if (len < 24 || data[0] != BRIDGE_PROTO_VERSION) {
|
||||
return;
|
||||
}
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
memset(s_info, 0, sizeof(s_info));
|
||||
memcpy(s_info, data, len < sizeof(s_info) ? len : sizeof(s_info));
|
||||
s_info_ms = now_ms();
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
static void ctl_send(uint8_t type, const uint8_t *body, size_t n)
|
||||
{
|
||||
uint8_t m[8];
|
||||
m[0] = type;
|
||||
if (n) {
|
||||
memcpy(m + 1, body, n);
|
||||
}
|
||||
eh_host_peer_data_send(RADIO_MSG_CTL, m, 1 + n);
|
||||
}
|
||||
|
||||
// Packs every queued frame into one peer-data message: each costs an SDIO round trip
|
||||
static void tx_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
static uint8_t batch[RADIO_BATCH_MAX];
|
||||
pkt_t p;
|
||||
for (;;) {
|
||||
xQueueReceive(s_txq, &p, portMAX_DELAY);
|
||||
size_t n = 0, frames = 0;
|
||||
do {
|
||||
memcpy(batch + n, p.mac, 6);
|
||||
batch[n + 6] = p.len;
|
||||
memcpy(batch + n + RADIO_ENTRY_HDR, p.data, p.len);
|
||||
n += RADIO_ENTRY_HDR + p.len;
|
||||
frames++;
|
||||
} while (n + RADIO_ENTRY_HDR + BRIDGE_PKT_MAX <= sizeof(batch) && xQueueReceive(s_txq, &p, 0) == pdTRUE);
|
||||
if (eh_host_peer_data_send(RADIO_MSG_TX, batch, n) == ESP_OK) {
|
||||
s_tx_msgs++;
|
||||
s_tx_frames += frames;
|
||||
} else {
|
||||
s_tx_drop += frames;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t queue_frame(const uint8_t *dst, const void *data, size_t len)
|
||||
{
|
||||
if (!s_started) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
if (len == 0 || len > BRIDGE_PKT_MAX) {
|
||||
return ESP_ERR_INVALID_SIZE;
|
||||
}
|
||||
pkt_t p;
|
||||
memcpy(p.mac, dst, 6);
|
||||
p.len = (uint8_t)len;
|
||||
memcpy(p.data, data, len);
|
||||
if (xQueueSend(s_txq, &p, 0) != pdTRUE) {
|
||||
s_tx_drop++;
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t bridge_hosted_send(const uint8_t *dst_mac, const void *data, size_t len)
|
||||
{
|
||||
if (desktop_active()) {
|
||||
s_own_dropped++;
|
||||
return ESP_OK; // the desktop has the radio: lost, as if on air
|
||||
}
|
||||
return queue_frame(dst_mac, data, len);
|
||||
}
|
||||
|
||||
esp_err_t bridge_hosted_set_channel(uint8_t channel)
|
||||
{
|
||||
if (!s_started || channel < 1 || channel > 13) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
ctl_send(BRIDGE_MSG_SET_CHANNEL, &channel, 1);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void bridge_hosted_set_rx(bridge_rx_cb_t cb)
|
||||
{
|
||||
s_rx_cb = cb;
|
||||
}
|
||||
|
||||
// HELLO every second keeps the INFO fresh; the desktop hand-over is logged here, once each way
|
||||
static void keepalive_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
for (;;) {
|
||||
ctl_send(BRIDGE_MSG_HELLO, NULL, 0);
|
||||
bool desk = desktop_active();
|
||||
if (desk != s_desk_logged) {
|
||||
s_desk_logged = desk;
|
||||
if (desk) {
|
||||
ESP_LOGW(TAG, "desktop " IPSTR " uses the radio: this controller's frames pause",
|
||||
IP2STR((esp_ip4_addr_t *)&s_desk.sin_addr.s_addr));
|
||||
} else {
|
||||
ESP_LOGI(TAG, "desktop gone quiet: this controller has the radio again");
|
||||
}
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
||||
size_t bridge_hosted_status(char *out, size_t n)
|
||||
{
|
||||
uint8_t info[BRIDGE_INFO_LEN];
|
||||
int64_t seen;
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
memcpy(info, s_info, sizeof(info));
|
||||
seen = s_info_ms;
|
||||
xSemaphoreGive(s_lock);
|
||||
bool linked = seen >= 0 && now_ms() - seen < INFO_TIMEOUT_MS;
|
||||
uint32_t c6_tx = info[8] | info[9] << 8 | info[10] << 16 | (uint32_t)info[11] << 24;
|
||||
uint32_t c6_fail = info[12] | info[13] << 8 | info[14] << 16 | (uint32_t)info[15] << 24;
|
||||
uint32_t c6_rx = info[16] | info[17] << 8 | info[18] << 16 | (uint32_t)info[19] << 24;
|
||||
uint32_t c6_drop = info[20] | info[21] << 8 | info[22] << 16 | (uint32_t)info[23] << 24;
|
||||
char desk[24] = "null";
|
||||
if (desktop_active()) {
|
||||
snprintf(desk, sizeof(desk), "\"" IPSTR "\"", IP2STR((esp_ip4_addr_t *)&s_desk.sin_addr.s_addr));
|
||||
}
|
||||
return (size_t)snprintf(out, n,
|
||||
"{\"port\":null,\"linked\":%s,\"mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\",\"channel\":%u,\"mic\":false,"
|
||||
"\"ip\":null,\"tx\":%lu,\"rx\":%lu,\"tx_fail\":%lu,\"drop\":%lu,\"sdio_msgs\":%lu,\"sdio_drop\":%lu,"
|
||||
"\"paused\":%lu,\"desktop\":%s}",
|
||||
linked ? "true" : "false", info[1], info[2], info[3], info[4], info[5], info[6], (unsigned)info[7],
|
||||
(unsigned long)c6_tx, (unsigned long)c6_rx, (unsigned long)c6_fail, (unsigned long)c6_drop,
|
||||
(unsigned long)s_tx_msgs, (unsigned long)s_tx_drop, (unsigned long)s_own_dropped, desk);
|
||||
}
|
||||
|
||||
esp_err_t bridge_hosted_start(void)
|
||||
{
|
||||
if (s_started) {
|
||||
return ESP_OK;
|
||||
}
|
||||
s_lock = xSemaphoreCreateMutex();
|
||||
s_txq = xQueueCreate(TXQ_LEN, sizeof(pkt_t));
|
||||
if (!s_lock || !s_txq) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
int rc = esp_hosted_connect_to_slave();
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "C6 not reachable over SDIO (%d): no ESP-NOW and no Wi-Fi", rc);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
c6_update_if_needed(); // restarts the board if it updated the C6
|
||||
if (eh_host_peer_data_register(RADIO_MSG_RX, on_rx, NULL) != ESP_OK ||
|
||||
eh_host_peer_data_register(RADIO_MSG_INFO, on_info, NULL) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "peer data handlers not registered");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
s_started = true;
|
||||
xTaskCreate(tx_task, "c6_tx", 4096, NULL, 6, NULL);
|
||||
xTaskCreate(keepalive_task, "c6_keep", 3072, NULL, 4, NULL);
|
||||
ESP_LOGI(TAG, "C6 radio link up");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/* ---- the UDP bridge service for a desktop ---------------------------------------------------------- */
|
||||
|
||||
static void build_udp_info(uint8_t *b)
|
||||
{
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
memcpy(b, s_info, BRIDGE_INFO_LEN);
|
||||
xSemaphoreGive(s_lock);
|
||||
b[0] = BRIDGE_PROTO_VERSION;
|
||||
b[24] = BRIDGE_CAP_MIC;
|
||||
memset(b + 25, 0, 4);
|
||||
esp_netif_t *eth = esp_netif_get_handle_from_ifkey("ETH_DEF");
|
||||
esp_netif_ip_info_t ip;
|
||||
if (eth && esp_netif_get_ip_info(eth, &ip) == ESP_OK && ip.ip.addr) {
|
||||
b[24] |= BRIDGE_CAP_ETH;
|
||||
memcpy(b + 25, &ip.ip.addr, 4); // network order: a.b.c.d
|
||||
}
|
||||
}
|
||||
|
||||
static void udp_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
uint8_t buf[6 + BRIDGE_PKT_MAX];
|
||||
for (;;) {
|
||||
struct sockaddr_in from;
|
||||
socklen_t fl = sizeof(from);
|
||||
int n = recvfrom(s_udp, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fl);
|
||||
if (n <= 6) {
|
||||
if (n < 0) {
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
s_desk = from;
|
||||
s_desk_ms = now_ms();
|
||||
queue_frame(buf, buf + 6, (size_t)(n - 6));
|
||||
}
|
||||
}
|
||||
|
||||
// The microphone to a desktop that asked for it (SET_MIC 1, repeated while it records)
|
||||
static void mic_udp_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
uint8_t out[1 + BRIDGE_AUDIO_BYTES];
|
||||
out[0] = BRIDGE_MSG_AUDIO;
|
||||
bool open = false;
|
||||
for (;;) {
|
||||
int64_t t = s_mic_ms;
|
||||
bool want = t >= 0 && now_ms() - t < MIC_UDP_TIMEOUT_MS;
|
||||
if (want && !open) {
|
||||
open = mic_stream_open() == ESP_OK; // busy while the web UI streams: try again later
|
||||
} else if (!want && open) {
|
||||
mic_stream_close();
|
||||
open = false;
|
||||
}
|
||||
if (!open) {
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
continue;
|
||||
}
|
||||
size_t got = 0;
|
||||
while (got < BRIDGE_AUDIO_BYTES) {
|
||||
size_t r = mic_stream_read(out + 1 + got, BRIDGE_AUDIO_BYTES - got, pdMS_TO_TICKS(100));
|
||||
if (r == 0) {
|
||||
break;
|
||||
}
|
||||
got += r;
|
||||
}
|
||||
if (got == BRIDGE_AUDIO_BYTES) {
|
||||
sendto(s_ctl, out, sizeof(out), 0, (const struct sockaddr *)&s_mic_peer, sizeof(s_mic_peer));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ctl_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
uint8_t buf[32];
|
||||
for (;;) {
|
||||
struct sockaddr_in from;
|
||||
socklen_t fl = sizeof(from);
|
||||
int n = recvfrom(s_ctl, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fl);
|
||||
if (n < 1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
continue;
|
||||
}
|
||||
if (buf[0] == BRIDGE_MSG_HELLO) {
|
||||
uint8_t out[1 + BRIDGE_INFO_LEN];
|
||||
out[0] = BRIDGE_MSG_INFO;
|
||||
build_udp_info(out + 1);
|
||||
sendto(s_ctl, out, sizeof(out), 0, (struct sockaddr *)&from, fl);
|
||||
} else if (buf[0] == BRIDGE_MSG_SET_MIC && n >= 2) {
|
||||
s_mic_peer = from;
|
||||
s_mic_ms = buf[1] ? now_ms() : -1;
|
||||
} else if (buf[0] == BRIDGE_MSG_SET_CHANNEL && n >= 2) {
|
||||
bridge_hosted_set_channel(buf[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int udp_bind(int port)
|
||||
{
|
||||
int s = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(port), .sin_addr.s_addr = htonl(INADDR_ANY) };
|
||||
if (s < 0 || bind(s, (struct sockaddr *)&a, sizeof(a)) != 0) {
|
||||
ESP_LOGE(TAG, "UDP port %d not open", port);
|
||||
if (s >= 0) {
|
||||
close(s);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void bridge_hosted_udp_start(void)
|
||||
{
|
||||
if (s_udp >= 0 || !s_started) {
|
||||
return;
|
||||
}
|
||||
s_udp = udp_bind(BRIDGE_DEFAULT_PORT);
|
||||
s_ctl = udp_bind(BRIDGE_DEFAULT_PORT + 1);
|
||||
if (s_udp < 0 || s_ctl < 0) {
|
||||
return;
|
||||
}
|
||||
xTaskCreate(udp_task, "c6_udp", 4096, NULL, 5, NULL);
|
||||
xTaskCreate(ctl_task, "c6_udp_ctl", 4096, NULL, 5, NULL);
|
||||
xTaskCreate(mic_udp_task, "c6_udp_mic", 3072, NULL, 5, NULL);
|
||||
ESP_LOGI(TAG, "network bridge on UDP %d (control %d) for a desktop led-controller", BRIDGE_DEFAULT_PORT,
|
||||
BRIDGE_DEFAULT_PORT + 1);
|
||||
}
|
||||
#endif // CONFIG_IDF_TARGET_ESP32P4
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
// ESP32-P4 only: the ESP32-C6 on the board as the controller's radio. The C6 runs radio-c6/ (esp-hosted plus
|
||||
// ESP-NOW) and the two talk over esp-hosted peer data (bridge/main/radio_proto.h). bridge.c runs it as the
|
||||
// bridge "c6", always there on this target, so every ESP-NOW frame goes the bridge way.
|
||||
//
|
||||
// The P4 also answers the network bridge protocol on UDP BRIDGE_DEFAULT_PORT (and the control port above it),
|
||||
// like a bridge board with Ethernet, so a desktop led-controller can use this radio. While a desktop is
|
||||
// sending, the controller's own frames are dropped, so the drivers hear one controller.
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "esp_err.h"
|
||||
#include "bridge.h"
|
||||
|
||||
#define BRIDGE_HOSTED_ADDR "c6"
|
||||
|
||||
// Connect to the C6 and start the link (call once, before espnow_tx_init). Brings the C6 up to the firmware in
|
||||
// the slave_fw partition first; that restarts the board when it updates.
|
||||
esp_err_t bridge_hosted_start(void);
|
||||
// Frames heard by the C6 go to cb (bridge.c's deliver)
|
||||
void bridge_hosted_set_rx(bridge_rx_cb_t cb);
|
||||
// The controller's frames: dropped while a desktop uses the radio
|
||||
esp_err_t bridge_hosted_send(const uint8_t *dst_mac, const void *data, size_t len);
|
||||
esp_err_t bridge_hosted_set_channel(uint8_t channel);
|
||||
// JSON object for the bridge status: linked, MAC, channel, counters, desktop
|
||||
size_t bridge_hosted_status(char *out, size_t n);
|
||||
// Open the UDP bridge service (once the network is up; safe to call again)
|
||||
void bridge_hosted_udp_start(void);
|
||||
@@ -0,0 +1,150 @@
|
||||
#include "sdkconfig.h"
|
||||
#if CONFIG_IDF_TARGET_ESP32P4
|
||||
#include "c6_update.h"
|
||||
|
||||
#include <string.h>
|
||||
#include "esp_app_desc.h"
|
||||
#include "esp_hosted.h"
|
||||
#include "esp_hosted_ota.h"
|
||||
#include "esp_image_format.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_partition.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs.h"
|
||||
#include "eh_host_feat_peer_data.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "radio_proto.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "c6_update";
|
||||
|
||||
#define SLAVE_FW_SUBTYPE 0x40
|
||||
#define CHUNK 1400
|
||||
|
||||
// Size of an app image: header, segments, checksum padded to 16, optional SHA-256. 0 if it is not one.
|
||||
static uint32_t image_size(const esp_partition_t *p)
|
||||
{
|
||||
esp_image_header_t h;
|
||||
if (esp_partition_read(p, 0, &h, sizeof h) != ESP_OK || h.magic != ESP_IMAGE_HEADER_MAGIC) {
|
||||
return 0;
|
||||
}
|
||||
uint32_t off = sizeof h;
|
||||
for (int i = 0; i < h.segment_count; i++) {
|
||||
esp_image_segment_header_t s;
|
||||
if (esp_partition_read(p, off, &s, sizeof s) != ESP_OK || s.data_len > p->size) {
|
||||
return 0;
|
||||
}
|
||||
off += sizeof s + s.data_len;
|
||||
}
|
||||
off = (off + 1 + 15) & ~15u; // the checksum byte, padded to 16
|
||||
if (h.hash_appended) {
|
||||
off += 32;
|
||||
}
|
||||
return off <= p->size ? off : 0;
|
||||
}
|
||||
|
||||
static char s_cur[32];
|
||||
static SemaphoreHandle_t s_got;
|
||||
|
||||
static void on_version(uint32_t id, const uint8_t *data, size_t len, void *ctx)
|
||||
{
|
||||
(void)id;
|
||||
(void)ctx;
|
||||
size_t n = len < sizeof(s_cur) - 1 ? len : sizeof(s_cur) - 1;
|
||||
memcpy(s_cur, data, n);
|
||||
s_cur[n] = 0;
|
||||
xSemaphoreGive(s_got);
|
||||
}
|
||||
|
||||
// The version radio-c6 reports over peer data; false if the C6 does not answer (it runs something else)
|
||||
static bool c6_version(char *out, size_t n)
|
||||
{
|
||||
s_got = xSemaphoreCreateBinary();
|
||||
if (!s_got || eh_host_peer_data_register(RADIO_MSG_VERSION, on_version, NULL) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
uint8_t ask = RADIO_CTL_VERSION;
|
||||
bool ok = false;
|
||||
for (int i = 0; i < 3 && !ok; i++) { // an unregistered msg id on the C6 is simply dropped
|
||||
eh_host_peer_data_send(RADIO_MSG_CTL, &ask, 1);
|
||||
ok = xSemaphoreTake(s_got, pdMS_TO_TICKS(500)) == pdTRUE;
|
||||
}
|
||||
if (ok) {
|
||||
strlcpy(out, s_cur, n);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
void c6_update_if_needed(void)
|
||||
{
|
||||
const esp_partition_t *p = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, SLAVE_FW_SUBTYPE, "slave_fw");
|
||||
if (!p) {
|
||||
ESP_LOGW(TAG, "no slave_fw partition: the C6 keeps its firmware");
|
||||
return;
|
||||
}
|
||||
uint32_t size = image_size(p);
|
||||
esp_app_desc_t img;
|
||||
if (!size || esp_partition_read(p, sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t), &img,
|
||||
sizeof img) != ESP_OK || img.magic_word != ESP_APP_DESC_MAGIC_WORD) {
|
||||
ESP_LOGW(TAG, "slave_fw holds no C6 image (make flash writes it): the C6 keeps its firmware");
|
||||
return;
|
||||
}
|
||||
char cur[32] = "";
|
||||
bool known = c6_version(cur, sizeof(cur));
|
||||
if (known && strncmp(cur, img.version, sizeof(cur)) == 0) {
|
||||
ESP_LOGI(TAG, "C6 runs %s, as in slave_fw", cur);
|
||||
return;
|
||||
}
|
||||
// One try per image: if the C6 still runs something else after it, say so and carry on rather than loop
|
||||
nvs_handle_t nv;
|
||||
char tried[32] = "";
|
||||
size_t tl = sizeof(tried);
|
||||
if (nvs_open("c6_update", NVS_READWRITE, &nv) != ESP_OK) {
|
||||
return;
|
||||
}
|
||||
if (nvs_get_str(nv, "tried", tried, &tl) == ESP_OK && strncmp(tried, img.version, sizeof(tried)) == 0) {
|
||||
ESP_LOGE(TAG, "C6 runs '%.32s' though '%.32s' was sent to it already: not trying again (erase NVS key "
|
||||
"c6_update/tried to retry)", known ? cur : "not radio-c6", img.version);
|
||||
nvs_close(nv);
|
||||
return;
|
||||
}
|
||||
nvs_set_str(nv, "tried", img.version);
|
||||
nvs_commit(nv);
|
||||
nvs_close(nv);
|
||||
|
||||
ESP_LOGW(TAG, "C6 runs '%.32s', slave_fw has '%.32s' (%u bytes): updating the C6",
|
||||
known ? cur : "not radio-c6", img.version, (unsigned)size);
|
||||
esp_err_t r = esp_hosted_cp_ota_begin();
|
||||
if (r != ESP_OK) {
|
||||
ESP_LOGE(TAG, "C6 OTA begin failed (%s): it keeps its firmware", esp_err_to_name(r));
|
||||
return;
|
||||
}
|
||||
static uint8_t buf[CHUNK];
|
||||
for (uint32_t off = 0; off < size;) {
|
||||
uint32_t n = size - off > CHUNK ? CHUNK : size - off;
|
||||
if (esp_partition_read(p, off, buf, n) != ESP_OK || esp_hosted_cp_ota_write(buf, n) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "C6 OTA write failed at %u: it keeps its firmware", (unsigned)off);
|
||||
esp_hosted_cp_ota_end();
|
||||
return;
|
||||
}
|
||||
off += n;
|
||||
if ((off / CHUNK) % 200 == 0 || off == size) {
|
||||
ESP_LOGI(TAG, "sent %u / %u bytes", (unsigned)off, (unsigned)size);
|
||||
}
|
||||
}
|
||||
r = esp_hosted_cp_ota_end();
|
||||
if (r != ESP_OK) {
|
||||
ESP_LOGE(TAG, "C6 OTA end failed (%s, image rejected?): it keeps its firmware", esp_err_to_name(r));
|
||||
return;
|
||||
}
|
||||
r = esp_hosted_cp_ota_activate();
|
||||
if (r != ESP_OK) {
|
||||
ESP_LOGE(TAG, "C6 OTA activate failed (%s): it keeps its firmware", esp_err_to_name(r));
|
||||
return;
|
||||
}
|
||||
ESP_LOGW(TAG, "C6 updated: restarting the board");
|
||||
vTaskDelay(pdMS_TO_TICKS(2000)); // the C6 restarts itself into the new image too
|
||||
esp_restart();
|
||||
}
|
||||
#endif // CONFIG_IDF_TARGET_ESP32P4
|
||||
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
// ESP32-P4 only: bring the C6 co-processor up to the radio-c6 image in the "slave_fw" partition (written by
|
||||
// make flash). Compares the version the C6 reports with the image's; on a difference it sends the image over
|
||||
// esp-hosted OTA and restarts the board, which resets the C6 into it. Call after esp_hosted_connect_to_slave.
|
||||
void c6_update_if_needed(void);
|
||||
+1
-1
@@ -498,7 +498,7 @@ static esp_err_t emit_zone(const char *zone_id, const uint8_t *frame, size_t len
|
||||
int n = engine_zone_targets(zone_id, t, ZONE_TARGETS_MAX);
|
||||
esp_err_t ret = n < 0 ? ESP_ERR_NOT_FOUND : ESP_OK;
|
||||
for (int i = 0; i < n; i++) {
|
||||
snprintf(s_gid, sizeof(s_gid), "%s", t[i]);
|
||||
strlcpy(s_gid, t[i], sizeof(s_gid));
|
||||
esp_err_t e = emit_c(NULL, frame, len, FRAME_REPEAT, commit);
|
||||
if (e != ESP_OK) {
|
||||
ret = e;
|
||||
|
||||
+13
-87
@@ -12,7 +12,6 @@
|
||||
#include "bridge.h"
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
#include "esp_netif.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_wifi_ap_get_sta_list.h"
|
||||
@@ -37,16 +36,11 @@ typedef struct {
|
||||
} bulk_t;
|
||||
|
||||
static QueueHandle_t s_bulk_q;
|
||||
static SemaphoreHandle_t s_bulk_done;
|
||||
static volatile bool s_bulk_ok;
|
||||
static volatile bool s_bulk_inflight;
|
||||
static volatile int64_t s_last_beat_us;
|
||||
static esp_timer_handle_t s_copy_timer;
|
||||
static beat_frame_t s_pending;
|
||||
static int s_copies_left;
|
||||
static uint16_t s_beat_idx;
|
||||
static uint8_t s_cur_peer[6];
|
||||
static bool s_have_peer;
|
||||
static espnow_stats_t s_stats;
|
||||
static volatile int s_loss_pct;
|
||||
static volatile bool s_loss_beats = true;
|
||||
@@ -63,17 +57,10 @@ void espnow_tx_set_bridges(const char addrs[][BRIDGE_ADDR_MAX], size_t n)
|
||||
s_nbridges = n;
|
||||
}
|
||||
|
||||
// One frame onto the air, or to the UDP bridge while one is set
|
||||
// One frame to the bridges: the P4 has no radio of its own, so even its C6 is one (bridge "c6")
|
||||
static esp_err_t radio_send(const uint8_t *dst, const void *data, size_t len)
|
||||
{
|
||||
if (bridge_active()) {
|
||||
return bridge_send(dst, data, len);
|
||||
}
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#else
|
||||
return esp_now_send(dst, data, len);
|
||||
#endif
|
||||
return bridge_active() ? bridge_send(dst, data, len) : ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
bool espnow_tx_drop(void)
|
||||
@@ -95,26 +82,6 @@ int espnow_tx_loss(bool *include_beats)
|
||||
return s_loss_pct;
|
||||
}
|
||||
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
static void IRAM_ATTR send_cb(const esp_now_send_info_t *info, esp_now_send_status_t status)
|
||||
{
|
||||
(void)info;
|
||||
if (s_bulk_inflight) {
|
||||
s_bulk_ok = status == ESP_NOW_SEND_SUCCESS;
|
||||
BaseType_t woken = pdFALSE;
|
||||
xSemaphoreGiveFromISR(s_bulk_done, &woken);
|
||||
portYIELD_FROM_ISR(woken);
|
||||
}
|
||||
}
|
||||
|
||||
static void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int len)
|
||||
{
|
||||
if (s_rx_cb) {
|
||||
s_rx_cb(info->src_addr, data, len);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void bridge_rx(const uint8_t *src, const uint8_t *data, int len)
|
||||
{
|
||||
if (s_rx_cb) {
|
||||
@@ -185,32 +152,6 @@ static void on_beat(const beat_info_t *info, float bpm, bool downbeat)
|
||||
espnow_tx_beat(bpm, downbeat, info->bar_sure ? info->bar_beat : 0);
|
||||
}
|
||||
|
||||
static esp_err_t select_peer(const uint8_t *mac)
|
||||
{
|
||||
if (bridge_active()) {
|
||||
return ESP_OK; // no peer table behind the bridge
|
||||
}
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#else
|
||||
if (s_have_peer && memcmp(s_cur_peer, mac, 6) == 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
// ESP-NOW holds 20 peers and there can be more drivers, so keep one
|
||||
// rotating unicast peer plus the broadcast peer.
|
||||
if (s_have_peer) {
|
||||
esp_now_del_peer(s_cur_peer);
|
||||
s_have_peer = false;
|
||||
}
|
||||
esp_now_peer_info_t p = { .channel = 0, .ifidx = WIFI_IF_STA, .encrypt = false };
|
||||
memcpy(p.peer_addr, mac, 6);
|
||||
ESP_RETURN_ON_ERROR(esp_now_add_peer(&p), TAG, "add peer");
|
||||
memcpy(s_cur_peer, mac, 6);
|
||||
s_have_peer = true;
|
||||
return ESP_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void bulk_task(void *arg)
|
||||
{
|
||||
bulk_t b;
|
||||
@@ -224,10 +165,6 @@ static void bulk_task(void *arg)
|
||||
}
|
||||
|
||||
const uint8_t *dst = b.broadcast ? BROADCAST : b.mac;
|
||||
if (!b.broadcast && select_peer(b.mac) != ESP_OK) {
|
||||
s_stats.bulk_failed++;
|
||||
continue;
|
||||
}
|
||||
int tries = b.broadcast ? 1 : BULK_RETRIES;
|
||||
bool ok = false;
|
||||
for (int t = 0; t < tries && !ok; t++) {
|
||||
@@ -235,18 +172,12 @@ static void bulk_task(void *arg)
|
||||
s_stats.bulk_retries++;
|
||||
vTaskDelay(pdMS_TO_TICKS(5 << t));
|
||||
}
|
||||
xSemaphoreTake(s_bulk_done, 0);
|
||||
s_bulk_inflight = true;
|
||||
if (espnow_tx_drop()) {
|
||||
s_stats.sim_dropped++; // lossy-link test: pretend the radio sent it
|
||||
ok = true;
|
||||
} else if (bridge_active()) {
|
||||
ok = bridge_send(dst, b.data, b.len) == ESP_OK; // no MAC-layer ack: rel.c's acks retry
|
||||
} else if (radio_send(dst, b.data, b.len) == ESP_OK &&
|
||||
xSemaphoreTake(s_bulk_done, pdMS_TO_TICKS(30)) == pdTRUE) {
|
||||
ok = s_bulk_ok || b.broadcast;
|
||||
} else {
|
||||
ok = radio_send(dst, b.data, b.len) == ESP_OK; // no MAC-layer ack: rel.c's acks retry
|
||||
}
|
||||
s_bulk_inflight = false;
|
||||
}
|
||||
s_stats.bulk_sent += ok;
|
||||
s_stats.bulk_failed += !ok;
|
||||
@@ -337,13 +268,8 @@ static esp_err_t radio_init(const char *ap_ssid, const char *ap_password)
|
||||
ESP_RETURN_ON_ERROR(esp_wifi_set_max_tx_power(80), TAG, "tx power"); // 20 dBm
|
||||
ESP_RETURN_ON_ERROR(esp_wifi_set_channel(CONFIG_ESPNOW_CHANNEL, WIFI_SECOND_CHAN_NONE), TAG, "channel");
|
||||
|
||||
ESP_RETURN_ON_ERROR(esp_now_init(), TAG, "espnow init");
|
||||
ESP_RETURN_ON_ERROR(esp_now_register_send_cb(send_cb), TAG, "send cb");
|
||||
esp_now_peer_info_t bc = { .channel = 0, .ifidx = WIFI_IF_STA, .encrypt = false };
|
||||
memcpy(bc.peer_addr, BROADCAST, 6);
|
||||
ESP_RETURN_ON_ERROR(esp_now_add_peer(&bc), TAG, "broadcast peer");
|
||||
ESP_RETURN_ON_ERROR(esp_now_register_recv_cb(recv_cb), TAG, "recv cb");
|
||||
ESP_LOGI(TAG, "ESP-NOW up on channel %d, %d beat copies", CONFIG_ESPNOW_CHANNEL, CONFIG_BEAT_TX_COPIES);
|
||||
// Wi-Fi runs on the C6 (esp-hosted); ESP-NOW there is radio-c6's, reached as the bridge "c6"
|
||||
ESP_LOGI(TAG, "Wi-Fi on the C6, channel %d; ESP-NOW through the C6 bridge", CONFIG_ESPNOW_CHANNEL);
|
||||
return ESP_OK;
|
||||
}
|
||||
#endif
|
||||
@@ -368,16 +294,16 @@ esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
||||
(void)ap_password;
|
||||
ESP_RETURN_ON_FALSE(s_nbridges, ESP_ERR_INVALID_STATE, TAG, "the host build needs a bridge address");
|
||||
#else
|
||||
// The radio still comes up behind a bridge: the access point serves the web UI
|
||||
ESP_RETURN_ON_ERROR(radio_init(ap_ssid, ap_password), TAG, "radio");
|
||||
#endif
|
||||
if (s_nbridges) {
|
||||
ESP_RETURN_ON_ERROR(bridge_set_all(s_bridges, s_nbridges, bridge_rx), TAG, "bridge");
|
||||
// The access point serves the web UI
|
||||
if (radio_init(ap_ssid, ap_password) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Wi-Fi on the C6 did not start: no access point"); // Ethernet and the rest still run
|
||||
}
|
||||
#endif
|
||||
// On the P4 the C6 radio is a bridge, so there is always one (bridge_set_all adds it)
|
||||
ESP_RETURN_ON_ERROR(bridge_set_all(s_bridges, s_nbridges, bridge_rx), TAG, "bridge");
|
||||
|
||||
s_bulk_q = xQueueCreate(BULK_QUEUE_LEN, sizeof(bulk_t));
|
||||
s_bulk_done = xSemaphoreCreateBinary();
|
||||
ESP_RETURN_ON_FALSE(s_bulk_q && s_bulk_done, ESP_ERR_NO_MEM, TAG, "alloc");
|
||||
ESP_RETURN_ON_FALSE(s_bulk_q, ESP_ERR_NO_MEM, TAG, "alloc");
|
||||
const esp_timer_create_args_t ta = { .callback = send_beat_copy, .name = "beat_copy",
|
||||
.dispatch_method = ESP_TIMER_TASK };
|
||||
ESP_RETURN_ON_ERROR(esp_timer_create(&ta, &s_copy_timer), TAG, "timer");
|
||||
|
||||
+11
-2
@@ -9,7 +9,16 @@ dependencies:
|
||||
- if: "target != linux"
|
||||
espressif/cjson:
|
||||
version: "^1.7.19"
|
||||
espressif/w5500:
|
||||
version: "^2.0.1"
|
||||
# Wi-Fi and ESP-NOW on the C6 over SDIO, and the ES8311 microphone codec
|
||||
espressif/esp_hosted:
|
||||
version: "~3.1.0"
|
||||
rules:
|
||||
- if: "target != linux"
|
||||
espressif/esp_wifi_remote:
|
||||
version: "*"
|
||||
rules:
|
||||
- if: "target != linux"
|
||||
espressif/esp_codec_dev:
|
||||
version: "^1.3.0"
|
||||
rules:
|
||||
- if: "target != linux"
|
||||
+33
-69
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* v2 controller: W5500 Ethernet, INMP441 beat detection, ESP-NOW master.
|
||||
* v2 controller on the Waveshare ESP32-P4-Nano: Ethernet on the internal EMAC (IP101), beat detection from the
|
||||
* ES8311 microphone, and Wi-Fi and ESP-NOW on the C6 next to it (bridge_hosted.c). See docs/P4_NANO.md.
|
||||
*/
|
||||
#include "auth.h"
|
||||
#include <stdio.h>
|
||||
@@ -12,12 +13,10 @@
|
||||
#include <stdlib.h>
|
||||
#include "esp_netif.h"
|
||||
#include "esp_eth.h"
|
||||
#include "esp_eth_mac_w5500.h"
|
||||
#include "esp_eth_phy_w5500.h"
|
||||
#include "bridge_hosted.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_heap_caps.h"
|
||||
@@ -38,6 +37,7 @@
|
||||
#include "api.h"
|
||||
#include "engine.h"
|
||||
#include "seqplay.h"
|
||||
#include "sim_rig.h"
|
||||
#include "audio_api.h"
|
||||
#include "rel.h"
|
||||
#include "web.h"
|
||||
@@ -84,7 +84,7 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
mic_get_level(&level);
|
||||
espnow_tx_stats(&es);
|
||||
rel_stats(&rs);
|
||||
if (s_eth_netif) { // NULL when the W5500 did not start
|
||||
if (s_eth_netif) { // NULL when Ethernet did not start
|
||||
esp_netif_get_ip_info(s_eth_netif, &ip_info);
|
||||
esp_netif_get_mac(s_eth_netif, mac);
|
||||
}
|
||||
@@ -119,9 +119,9 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
#define DEFAULT_HOSTNAME "led-controller"
|
||||
|
||||
// Address when no DHCP server answers, as on a cable straight to a PC. The PC needs a static IP on this subnet.
|
||||
#define DEFAULT_IP ESP_IP4TOADDR(192, 168, 4, 1)
|
||||
#define DEFAULT_IP ESP_IP4TOADDR(CONFIG_P4_DEFAULT_IP_A, CONFIG_P4_DEFAULT_IP_B, CONFIG_P4_DEFAULT_IP_C, CONFIG_P4_DEFAULT_IP_D)
|
||||
#define DHCP_WAIT_S 5 // the Nano usually sits on a cable to the PC, with no DHCP server
|
||||
#define DEFAULT_NETMASK ESP_IP4TOADDR(255, 255, 255, 0)
|
||||
#define DHCP_WAIT_S 15
|
||||
|
||||
// Boolean setting; def when missing or not a boolean
|
||||
static bool settings_bool(const char *key, bool def)
|
||||
@@ -299,10 +299,10 @@ static void start_webserver(void)
|
||||
}
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.uri_match_fn = httpd_uri_match_wildcard;
|
||||
config.max_uri_handlers = 80;
|
||||
config.stack_size = 8192;
|
||||
config.max_uri_handlers = 112;
|
||||
config.stack_size = 12288; // the rig's room and flash files (sim_rig.c) need more than 8 KB
|
||||
config.lru_purge_enable = true;
|
||||
config.max_open_sockets = 12; // browsers open 6 connections at once
|
||||
config.max_open_sockets = 24; // browsers open 6 connections at once, and the rig page its own
|
||||
config.backlog_conn = 10; // the default 5 drops the 6th SYN, a 1 s retry
|
||||
if (httpd_start(&s_server, &config) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "HTTP server start failed");
|
||||
@@ -310,6 +310,7 @@ static void start_webserver(void)
|
||||
}
|
||||
const httpd_uri_t status_uri = { .uri = "/status", .method = HTTP_GET, .handler = status_get_handler };
|
||||
auth_register_uri(s_server, &status_uri);
|
||||
sim_rig_register(s_server); // before the pages: /sim/* is not theirs
|
||||
web_register(s_server);
|
||||
audio_register(s_server);
|
||||
audio_api_register(s_server);
|
||||
@@ -373,71 +374,28 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t w5500_init(esp_eth_handle_t *eth_handle_out)
|
||||
// The P4's own EMAC over RMII to the IP101 PHY: MDC 31, MDIO 52, 50 MHz reference in on GPIO 50 (IDF's P4
|
||||
// defaults, which match the Waveshare ESP32-P4-Nano), PHY reset on CONFIG_P4_ETH_PHY_RST_GPIO
|
||||
static esp_err_t eth_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.base.int_gpio_num = CONFIG_W5500_INT_GPIO;
|
||||
if (CONFIG_W5500_INT_GPIO < 0) {
|
||||
w5500_config.base.poll_period_ms = CONFIG_W5500_POLL_PERIOD_MS;
|
||||
}
|
||||
|
||||
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
|
||||
eth_esp32_emac_config_t emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
|
||||
emac_config.smi_gpio.mdc_num = CONFIG_P4_ETH_MDC_GPIO;
|
||||
emac_config.smi_gpio.mdio_num = CONFIG_P4_ETH_MDIO_GPIO;
|
||||
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");
|
||||
|
||||
phy_config.phy_addr = ESP_ETH_PHY_ADDR_AUTO;
|
||||
phy_config.reset_gpio_num = CONFIG_P4_ETH_PHY_RST_GPIO;
|
||||
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&emac_config, &mac_config);
|
||||
esp_eth_phy_t *phy = esp_eth_phy_new_generic(&phy_config); // the IP101 is a plain 802.3 PHY
|
||||
ESP_RETURN_ON_FALSE(mac && phy, ESP_FAIL, TAG, "EMAC/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) {
|
||||
esp_err_t err = esp_eth_driver_install(ð_config, eth_handle_out);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Ethernet driver install failed: %s", esp_err_to_name(err));
|
||||
mac->del(mac);
|
||||
}
|
||||
if (phy) {
|
||||
phy->del(phy);
|
||||
}
|
||||
spi_bus_free(CONFIG_W5500_SPI_HOST);
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
@@ -461,11 +419,17 @@ void app_main(void)
|
||||
ESP_LOGI(TAG, "AP %s%s", s_ap_on ? "on" : "off", held ? " (button held)" : "");
|
||||
char bridges[BRIDGE_MAX_LINKS][BRIDGE_ADDR_MAX]; // led-simulator or a network/USB bridge stands in for the radio
|
||||
espnow_tx_set_bridges(bridges, store_bridges(bridges, BRIDGE_MAX_LINKS));
|
||||
// The C6 is the radio: without it there is no Wi-Fi and no ESP-NOW, but the web UI still runs on Ethernet
|
||||
if (bridge_hosted_start() != ESP_OK) {
|
||||
ESP_LOGE(TAG, "C6 radio down: no Wi-Fi access point and no ESP-NOW");
|
||||
s_ap_on = false;
|
||||
}
|
||||
ESP_ERROR_CHECK(espnow_tx_init(s_ap_on ? (host ? host : DEFAULT_HOSTNAME) : NULL, settings_str("ap_password")));
|
||||
bridge_hosted_udp_start();
|
||||
|
||||
// Ethernet is optional: without it the web UI is still served over the AP
|
||||
esp_eth_handle_t eth_handle = NULL;
|
||||
if (w5500_init(ð_handle) == ESP_OK) {
|
||||
if (eth_init(ð_handle) == ESP_OK) {
|
||||
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)));
|
||||
|
||||
+71
-26
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* INMP441 I2S microphone: level meter plus a 16-bit PCM feed for streaming.
|
||||
* Reads both I2S slots and uses whichever one the mic is driving (L/R pin to
|
||||
* GND = left, to 3V3 = right), keeping a running RMS/peak for the last block.
|
||||
* The ESP32-P4-Nano's microphone (ES8311 codec over I2S): level meter plus a 16-bit PCM feed for streaming.
|
||||
* Reads both I2S slots and uses whichever one carries sound (the ES8311 puts it in the left one),
|
||||
* keeping a running RMS/peak for the last block.
|
||||
* Every block also goes to the beat detector.
|
||||
*/
|
||||
#include <math.h>
|
||||
@@ -12,6 +12,9 @@
|
||||
#include "esp_check.h"
|
||||
#include "driver/i2s_std.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/i2c_master.h"
|
||||
#include "esp_codec_dev.h"
|
||||
#include "esp_codec_dev_defaults.h"
|
||||
#include "mic.h"
|
||||
#include "beat.h"
|
||||
|
||||
@@ -19,7 +22,7 @@
|
||||
#define LEVEL_BLOCKS 5 // 100 ms per level reading
|
||||
#define FULL_SCALE_24BIT 8388608.0f
|
||||
#define DBFS_FLOOR -120.0f
|
||||
#define INMP441_SPL_OFFSET 120.0f // 94 dB SPL - (-26 dBFS)
|
||||
#define SPL_OFFSET 120.0f // 94 dB SPL - (-26 dBFS), an INMP441 figure: rough for the ES8311
|
||||
|
||||
static const char *TAG = "mic";
|
||||
|
||||
@@ -30,7 +33,7 @@ static portMUX_TYPE s_level_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
static StreamBufferHandle_t s_stream_buf;
|
||||
static volatile bool s_stream_active;
|
||||
|
||||
static mic_level_t s_level = { DBFS_FLOOR, DBFS_FLOOR, DBFS_FLOOR + INMP441_SPL_OFFSET };
|
||||
static mic_level_t s_level = { DBFS_FLOOR, DBFS_FLOOR, DBFS_FLOOR + SPL_OFFSET };
|
||||
|
||||
static float to_dbfs(float x)
|
||||
{
|
||||
@@ -42,7 +45,7 @@ static void mic_task(void *arg)
|
||||
static int32_t buf[MIC_BLOCK_SAMPLES * 2]; // interleaved L/R
|
||||
static int16_t pcm[MIC_BLOCK_SAMPLES];
|
||||
static float filtered[MIC_BLOCK_SAMPLES];
|
||||
// One-pole high-pass to strip the INMP441's DC offset
|
||||
// One-pole high-pass to strip the mic's DC offset
|
||||
float hp_prev_in = 0.0f, hp_prev_out = 0.0f;
|
||||
// Level is accumulated over several short blocks so the meter stays steady
|
||||
double level_sum_sq = 0.0;
|
||||
@@ -99,7 +102,7 @@ static void mic_task(void *arg)
|
||||
mic_level_t level;
|
||||
level.rms_dbfs = to_dbfs(sqrtf((float)(level_sum_sq / level_frames)));
|
||||
level.peak_dbfs = to_dbfs(level_peak);
|
||||
level.spl_db = level.rms_dbfs + INMP441_SPL_OFFSET;
|
||||
level.spl_db = level.rms_dbfs + SPL_OFFSET;
|
||||
level_sum_sq = 0.0;
|
||||
level_peak = 0.0f;
|
||||
level_frames = level_blocks = 0;
|
||||
@@ -117,6 +120,65 @@ static void mic_task(void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
// The Waveshare ESP32-P4-Nano's microphone goes through the ES8311 codec's ADC. The codec needs MCLK and its
|
||||
// registers set over I2C; esp_codec_dev does both and drives the I2S channel (s_rx_chan) as its data path, in the
|
||||
// 32-bit stereo frames mic_task reads.
|
||||
static esp_err_t codec_mic_start(void)
|
||||
{
|
||||
i2c_master_bus_handle_t bus;
|
||||
i2c_master_bus_config_t bc = {
|
||||
.i2c_port = I2C_NUM_0,
|
||||
.sda_io_num = CONFIG_P4_CODEC_I2C_SDA_GPIO,
|
||||
.scl_io_num = CONFIG_P4_CODEC_I2C_SCL_GPIO,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.flags.enable_internal_pullup = true,
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(i2c_new_master_bus(&bc, &bus), TAG, "I2C bus");
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(MIC_SAMPLE_RATE), // MCLK = 256 x fs
|
||||
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO),
|
||||
.gpio_cfg = {
|
||||
.mclk = CONFIG_P4_I2S_MCLK_GPIO,
|
||||
.bclk = CONFIG_P4_I2S_BCLK_GPIO,
|
||||
.ws = CONFIG_P4_I2S_WS_GPIO,
|
||||
.dout = I2S_GPIO_UNUSED,
|
||||
.din = CONFIG_P4_I2S_DIN_GPIO,
|
||||
},
|
||||
};
|
||||
ESP_RETURN_ON_ERROR(i2s_channel_init_std_mode(s_rx_chan, &std_cfg), TAG, "I2S std mode init failed");
|
||||
ESP_RETURN_ON_ERROR(i2s_channel_enable(s_rx_chan), TAG, "I2S enable failed"); // MCLK runs before the codec
|
||||
|
||||
audio_codec_i2s_cfg_t ic = { .port = I2S_NUM_0, .rx_handle = s_rx_chan, .tx_handle = NULL };
|
||||
const audio_codec_data_if_t *data_if = audio_codec_new_i2s_data(&ic);
|
||||
audio_codec_i2c_cfg_t cc = { .port = I2C_NUM_0, .addr = CONFIG_P4_CODEC_I2C_ADDR << 1, .bus_handle = bus };
|
||||
const audio_codec_ctrl_if_t *ctrl_if = audio_codec_new_i2c_ctrl(&cc);
|
||||
const audio_codec_gpio_if_t *gpio_if = audio_codec_new_gpio();
|
||||
ESP_RETURN_ON_FALSE(data_if && ctrl_if && gpio_if, ESP_ERR_NO_MEM, TAG, "codec interfaces");
|
||||
es8311_codec_cfg_t ec = {
|
||||
.ctrl_if = ctrl_if,
|
||||
.gpio_if = gpio_if,
|
||||
.codec_mode = ESP_CODEC_DEV_WORK_MODE_ADC,
|
||||
.pa_pin = -1,
|
||||
.use_mclk = true,
|
||||
};
|
||||
const audio_codec_if_t *codec_if = es8311_codec_new(&ec);
|
||||
ESP_RETURN_ON_FALSE(codec_if, ESP_FAIL, TAG, "ES8311 not found on I2C 0x%02x", CONFIG_P4_CODEC_I2C_ADDR);
|
||||
esp_codec_dev_cfg_t dc = { .dev_type = ESP_CODEC_DEV_TYPE_IN, .codec_if = codec_if, .data_if = data_if };
|
||||
esp_codec_dev_handle_t dev = esp_codec_dev_new(&dc);
|
||||
ESP_RETURN_ON_FALSE(dev, ESP_ERR_NO_MEM, TAG, "codec device");
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.bits_per_sample = 32,
|
||||
.channel = 2,
|
||||
.sample_rate = MIC_SAMPLE_RATE,
|
||||
.mclk_multiple = 256,
|
||||
};
|
||||
ESP_RETURN_ON_FALSE(esp_codec_dev_open(dev, &fs) == ESP_CODEC_DEV_OK, ESP_FAIL, TAG, "codec open");
|
||||
esp_codec_dev_set_in_gain(dev, CONFIG_P4_MIC_PGA_DB);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t mic_start(void)
|
||||
{
|
||||
s_stream_buf = xStreamBufferCreate(STREAM_BUF_BYTES, 1);
|
||||
@@ -124,30 +186,13 @@ esp_err_t mic_start(void)
|
||||
|
||||
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER);
|
||||
ESP_RETURN_ON_ERROR(i2s_new_channel(&chan_cfg, NULL, &s_rx_chan), TAG, "I2S channel create failed");
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(MIC_SAMPLE_RATE),
|
||||
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO),
|
||||
.gpio_cfg = {
|
||||
.mclk = I2S_GPIO_UNUSED,
|
||||
.bclk = CONFIG_MIC_SCK_GPIO,
|
||||
.ws = CONFIG_MIC_WS_GPIO,
|
||||
.dout = I2S_GPIO_UNUSED,
|
||||
.din = CONFIG_MIC_SD_GPIO,
|
||||
},
|
||||
};
|
||||
|
||||
ESP_RETURN_ON_ERROR(i2s_channel_init_std_mode(s_rx_chan, &std_cfg), TAG, "I2S std mode init failed");
|
||||
// SD tri-states outside the mic's slot; the datasheet asks for a pull-down
|
||||
gpio_pullup_dis(CONFIG_MIC_SD_GPIO);
|
||||
gpio_pulldown_en(CONFIG_MIC_SD_GPIO);
|
||||
ESP_RETURN_ON_ERROR(i2s_channel_enable(s_rx_chan), TAG, "I2S enable failed");
|
||||
ESP_RETURN_ON_ERROR(codec_mic_start(), TAG, "ES8311 start failed");
|
||||
|
||||
ESP_RETURN_ON_ERROR(beat_start(MIC_SAMPLE_RATE), TAG, "Beat detector start failed");
|
||||
if (xTaskCreate(mic_task, "mic", 6144, NULL, 5, NULL) != pdPASS) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
ESP_LOGI(TAG, "INMP441 started at %d Hz", MIC_SAMPLE_RATE);
|
||||
ESP_LOGI(TAG, "ES8311 microphone started at %d Hz", MIC_SAMPLE_RATE);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -10,10 +10,10 @@
|
||||
typedef struct {
|
||||
float rms_dbfs; // RMS level over the last block, dB relative to full scale
|
||||
float peak_dbfs; // Peak sample in the last block, dBFS
|
||||
float spl_db; // Approximate dB SPL (INMP441: -26 dBFS @ 94 dB SPL)
|
||||
float spl_db; // Approximate dB SPL (an INMP441 calibration: -26 dBFS @ 94 dB SPL)
|
||||
} mic_level_t;
|
||||
|
||||
// Start the INMP441 I2S capture and the level-measuring task
|
||||
// Start the ES8311 microphone capture and the level-measuring task
|
||||
esp_err_t mic_start(void);
|
||||
|
||||
// Latest level reading; returns -120 dBFS until the first block arrives
|
||||
|
||||
Reference in new issue
Block a user