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:
jimmyandClaude Opus 5.5 committed 2026-11-24 21:45:25 +13:00
1 parent ba8e160aee
commit 4ab0c7c6d7
101 files changed
+3284 -971

No files matched your search

+13 -87
View File
@@ -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");