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

+5
View File
@@ -0,0 +1,5 @@
build/
sdkconfig
sdkconfig.old
managed_components/
dependencies.lock
+19
View File
@@ -0,0 +1,19 @@
# ESP-NOW radio for the ESP32-P4-Nano: esp-hosted co-processor firmware for its ESP32-C6, plus ESP-NOW over
# esp-hosted peer data (main/radio_now.c). The P4 controller carries this image and updates the C6 when the
# version differs (main/c6_update.c), so the version is a hash of what goes into it.
cmake_minimum_required(VERSION 3.16)
set(_ver_src ${CMAKE_CURRENT_LIST_DIR}/main/radio_now.c ${CMAKE_CURRENT_LIST_DIR}/../bridge/main/radio_proto.h
${CMAKE_CURRENT_LIST_DIR}/../bridge/main/bridge_proto.h ${CMAKE_CURRENT_LIST_DIR}/sdkconfig.defaults
${CMAKE_CURRENT_LIST_DIR}/main/idf_component.yml)
set(_ver_all "")
foreach(f ${_ver_src})
file(MD5 ${f} _h)
string(APPEND _ver_all ${_h})
endforeach()
string(MD5 _ver_hash "${_ver_all}")
string(SUBSTRING ${_ver_hash} 0 10 _ver_short)
set(PROJECT_VER "rc6-${_ver_short}")
# Re-run CMake (and so recompute the version) whenever one of these changes, not only on a fresh build
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${_ver_src})
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(radio_c6)
+3
View File
@@ -0,0 +1,3 @@
idf_component_register(SRCS "radio_now.c"
INCLUDE_DIRS "." "../../bridge/main"
PRIV_REQUIRES esp_hosted nvs_flash esp_wifi esp_event esp_app_format)
+3
View File
@@ -0,0 +1,3 @@
dependencies:
espressif/esp_hosted:
version: "~3.1.0"
+263
View File
@@ -0,0 +1,263 @@
/*
* ESP-NOW radio for the ESP32-P4-Nano's controller. The C6 runs esp-hosted (the P4's Wi-Fi over SDIO), and
* this file adds what esp-hosted does not forward: ESP-NOW. Frames arrive from the P4 in peer-data batches
* (bridge/main/radio_proto.h), go out one at a time, and every frame heard goes back up in batches.
* Same job as the USB bridge in bridge/, over SDIO instead of USB.
*/
#include <string.h>
#include "eh_cp_feat_peer_data.h"
#include "esp_app_desc.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_now.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "bridge_proto.h"
#include "radio_proto.h"
static const char *TAG = "radio_now";
#define PEERS_MAX 19 // ESP-NOW allows 20 peers: 19 unicast and the broadcast one
#define TX_QUEUE_LEN 64
#define RX_QUEUE_LEN 64
#define SEND_WAIT_MS 100
typedef struct {
uint8_t mac[6];
uint8_t len;
uint8_t data[BRIDGE_PKT_MAX];
} pkt_t;
static const uint8_t BROADCAST[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static QueueHandle_t s_txq;
static QueueHandle_t s_rxq;
static SemaphoreHandle_t s_sent;
static volatile bool s_now_up;
static uint8_t s_peers[PEERS_MAX][6]; // most recently used first
static int s_npeers;
static uint32_t s_tx, s_tx_fail, s_rx, s_drop;
/* ---- ESP-NOW ----------------------------------------------------------------------------------- */
static void on_sent(const esp_now_send_info_t *info, esp_now_send_status_t status)
{
(void)info;
if (status != ESP_NOW_SEND_SUCCESS) {
s_tx_fail++;
}
xSemaphoreGive(s_sent);
}
static void on_recv(const esp_now_recv_info_t *info, const uint8_t *data, int len)
{
if (len <= 0 || len > BRIDGE_PKT_MAX) {
return;
}
pkt_t p;
memcpy(p.mac, info->src_addr, 6);
p.len = (uint8_t)len;
memcpy(p.data, data, len);
if (xQueueSend(s_rxq, &p, 0) == pdTRUE) {
s_rx++;
} else {
s_drop++;
}
}
// Keep the peer table within the ESP-NOW limit, least recently used dropped first (as bridge/ does)
static void ensure_peer(const uint8_t *mac)
{
if (memcmp(mac, BROADCAST, 6) == 0) {
if (!esp_now_is_peer_exist(BROADCAST)) {
esp_now_peer_info_t p = { .channel = 0, .ifidx = WIFI_IF_STA, .encrypt = false };
memcpy(p.peer_addr, BROADCAST, 6);
esp_now_add_peer(&p);
}
return;
}
for (int i = 0; i < s_npeers; i++) {
if (memcmp(s_peers[i], mac, 6) == 0) {
uint8_t hit[6];
memcpy(hit, mac, 6);
memmove(s_peers[1], s_peers[0], i * 6);
memcpy(s_peers[0], hit, 6);
return;
}
}
if (s_npeers == PEERS_MAX) {
esp_now_del_peer(s_peers[PEERS_MAX - 1]);
s_npeers--;
}
esp_now_peer_info_t p = { .channel = 0, .ifidx = WIFI_IF_STA, .encrypt = false };
memcpy(p.peer_addr, mac, 6);
if (esp_now_add_peer(&p) != ESP_OK) {
return;
}
memmove(s_peers[1], s_peers[0], s_npeers * 6);
memcpy(s_peers[0], mac, 6);
s_npeers++;
}
// The P4 starts and stops Wi-Fi over esp-hosted; ESP-NOW follows it
static void on_wifi(void *arg, esp_event_base_t base, int32_t id, void *data)
{
(void)arg;
(void)base;
(void)data;
if ((id == WIFI_EVENT_STA_START || id == WIFI_EVENT_AP_START) && !s_now_up) {
if (esp_now_init() == ESP_OK) {
esp_now_register_send_cb(on_sent);
esp_now_register_recv_cb(on_recv);
s_npeers = 0;
s_now_up = true;
uint8_t ch;
wifi_second_chan_t sc;
esp_wifi_get_channel(&ch, &sc);
ESP_LOGI(TAG, "ESP-NOW up on channel %u", ch);
} else {
ESP_LOGE(TAG, "esp_now_init failed");
}
} else if (id == WIFI_EVENT_STA_STOP && s_now_up) {
s_now_up = false;
esp_now_deinit();
ESP_LOGI(TAG, "ESP-NOW down (Wi-Fi stopped)");
}
}
static void tx_task(void *arg)
{
(void)arg;
pkt_t p;
for (;;) {
xQueueReceive(s_txq, &p, portMAX_DELAY);
if (!s_now_up) {
s_tx_fail++;
continue;
}
ensure_peer(p.mac);
xSemaphoreTake(s_sent, 0);
if (esp_now_send(p.mac, p.data, p.len) != ESP_OK) {
s_tx_fail++;
continue;
}
s_tx++;
xSemaphoreTake(s_sent, pdMS_TO_TICKS(SEND_WAIT_MS));
}
}
// Everything heard, packed into as few peer-data messages as the queue allows
static void rx_task(void *arg)
{
(void)arg;
static uint8_t batch[RADIO_BATCH_MAX];
pkt_t p;
for (;;) {
xQueueReceive(s_rxq, &p, portMAX_DELAY);
size_t n = 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;
} while (n + RADIO_ENTRY_HDR + BRIDGE_PKT_MAX <= sizeof(batch) && xQueueReceive(s_rxq, &p, 0) == pdTRUE);
if (eh_cp_feat_peer_data_send(RADIO_MSG_RX, batch, n) != ESP_OK) {
s_drop++;
}
}
}
/* ---- the P4's messages -------------------------------------------------------------------------- */
static void put_u32(uint8_t *p, uint32_t v)
{
p[0] = v & 0xFF;
p[1] = (v >> 8) & 0xFF;
p[2] = (v >> 16) & 0xFF;
p[3] = (v >> 24) & 0xFF;
}
static void send_info(void)
{
uint8_t b[BRIDGE_INFO_LEN] = { 0 };
uint8_t ch = 0;
wifi_second_chan_t sc;
b[0] = BRIDGE_PROTO_VERSION;
esp_read_mac(b + 1, ESP_MAC_WIFI_STA);
if (esp_wifi_get_channel(&ch, &sc) == ESP_OK) {
b[7] = ch;
}
put_u32(b + 8, s_tx);
put_u32(b + 12, s_tx_fail);
put_u32(b + 16, s_rx);
put_u32(b + 20, s_drop);
eh_cp_feat_peer_data_send(RADIO_MSG_INFO, b, sizeof(b));
}
// Runs in the RPC receive context: only queue, no waiting
static void on_tx(uint32_t id, const uint8_t *data, size_t len, void *ctx)
{
(void)id;
(void)ctx;
size_t off = 0;
while (off + RADIO_ENTRY_HDR <= len) {
pkt_t p;
memcpy(p.mac, data + off, 6);
p.len = data[off + 6];
if (p.len == 0 || p.len > BRIDGE_PKT_MAX || off + RADIO_ENTRY_HDR + p.len > len) {
break;
}
memcpy(p.data, data + off + RADIO_ENTRY_HDR, p.len);
off += RADIO_ENTRY_HDR + p.len;
if (xQueueSend(s_txq, &p, 0) != pdTRUE) {
s_tx_fail++;
}
}
}
static void on_ctl(uint32_t id, const uint8_t *data, size_t len, void *ctx)
{
(void)id;
(void)ctx;
if (len < 1) {
return;
}
if (data[0] == BRIDGE_MSG_SET_CHANNEL && len >= 2 && data[1] >= 1 && data[1] <= 13) {
esp_wifi_set_channel(data[1], WIFI_SECOND_CHAN_NONE);
ESP_LOGI(TAG, "channel %u", data[1]);
}
if (data[0] == BRIDGE_MSG_SET_CHANNEL || data[0] == BRIDGE_MSG_HELLO) {
send_info();
} else if (data[0] == RADIO_CTL_VERSION) {
const char *v = esp_app_get_description()->version;
eh_cp_feat_peer_data_send(RADIO_MSG_VERSION, (const uint8_t *)v, strlen(v));
}
}
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(esp_event_loop_create_default());
s_txq = xQueueCreate(TX_QUEUE_LEN, sizeof(pkt_t));
s_rxq = xQueueCreate(RX_QUEUE_LEN, sizeof(pkt_t));
s_sent = xSemaphoreCreateBinary();
ESP_ERROR_CHECK(s_txq && s_rxq && s_sent ? ESP_OK : ESP_ERR_NO_MEM);
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, on_wifi, NULL));
ESP_ERROR_CHECK(eh_cp_feat_peer_data_register_callback(RADIO_MSG_TX, on_tx, NULL));
ESP_ERROR_CHECK(eh_cp_feat_peer_data_register_callback(RADIO_MSG_CTL, on_ctl, NULL));
xTaskCreate(tx_task, "now_tx", 3072, NULL, 6, NULL);
xTaskCreate(rx_task, "now_rx", 3072, NULL, 5, NULL);
ESP_LOGI(TAG, "radio-c6 %s ready", esp_app_get_description()->version);
}
+7
View File
@@ -0,0 +1,7 @@
# ESP-Hosted shared partition table — 4 MB flash, dual OTA (1.75 MB per slot)
# Name, Type, SubType, Offset, Size
nvs, data, nvs, 0x9000, 0x4000
otadata, data, ota, 0xd000, 0x2000
phy_init, data, phy, 0xf000, 0x1000
ota_0, app, ota_0, 0x10000, 0x1C0000
ota_1, app, ota_1, 0x1D0000, 0x1C0000
1 # ESP-Hosted shared partition table — 4 MB flash, dual OTA (1.75 MB per slot)
2 # Name, Type, SubType, Offset, Size
3 nvs, data, nvs, 0x9000, 0x4000
4 otadata, data, ota, 0xd000, 0x2000
5 phy_init, data, phy, 0xf000, 0x1000
6 ota_0, app, ota_0, 0x10000, 0x1C0000
7 ota_1, app, ota_1, 0x1D0000, 0x1C0000
+16
View File
@@ -0,0 +1,16 @@
# ESP32-C6 on the Waveshare ESP32-P4-Nano: esp-hosted co-processor over SDIO for the P4, plus ESP-NOW
CONFIG_IDF_TARGET="esp32c6"
CONFIG_ESP_HOSTED=y
CONFIG_ESP_HOSTED_CP=y
CONFIG_ESP_HOSTED_CP_FOR_MCU=y
CONFIG_ESP_HOSTED_CP_RPC_V2=y
CONFIG_ESP_HOSTED_CP_FEAT_WIFI=y
CONFIG_ESP_HOSTED_CP_FEAT_PEER_DATA=y
CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT=n
CONFIG_BT_ENABLED=n
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_eh_cp_ota_4m.csv"
CONFIG_FREERTOS_HZ=1000
CONFIG_ESP_WIFI_NVS_ENABLED=y
CONFIG_ESP_WIFI_ESPNOW_MAX_ENCRYPT_NUM=7
+100
View File
@@ -0,0 +1,100 @@
# 1. CPU
#--------
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_160=y
# 2. BT Configuration
#---------------------
CONFIG_BT_ENABLED=n
CONFIG_BT_CONTROLLER_ONLY=y
CONFIG_BT_BLUEDROID_ENABLED=
CONFIG_BT_LE_SLEEP_ENABLE=y
# 3.1 BLE over SPI/SDIO
#-----------------------
CONFIG_BT_LE_HCI_INTERFACE_USE_RAM=y
# 3.2 BLE over UART
#-------------------
# Enable below config, delete sdkconfig, build/ and rebuild
# CONFIG_BT_LE_HCI_INTERFACE_USE_UART=y
# CONFIG_BT_LE_HCI_UART_TX_PIN=5
# CONFIG_BT_LE_HCI_UART_RX_PIN=12
# CONFIG_BT_LE_HCI_UART_FLOWCTRL=n
# # 4 pin solution is not working yet for c6
# #CONFIG_BT_LE_HCI_UART_RTS_PIN=9
# #CONFIG_BT_LE_HCI_UART_CTS_PIN=13
# 4. Partition Table
#--------------------
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_eh_cp_ota_4m.csv"
# 5. Mempool
#------------
CONFIG_ESP_CACHE_MALLOC=y
# # 6.1 LWIP split
# #---------------
#
# CONFIG_ESP_HOSTED_CP_FEAT_NW_SPLIT=y
# CONFIG_LWIP_TCP_LOCAL_PORT_RANGE_START=61440
# CONFIG_LWIP_TCP_LOCAL_PORT_RANGE_END=65535
# CONFIG_LWIP_UDP_LOCAL_PORT_RANGE_START=61440
# CONFIG_LWIP_UDP_LOCAL_PORT_RANGE_END=65535
#
#
# # 6.2 LWIP split examples
# #-------------------------
# 7. Transport config
#---------------------
# ESP_HOSTED_TRANSPORT_CP_SDIO_NSEND_PSAMPLE=y
# ESP_HOSTED_TRANSPORT_CP_SDIO_DEFAULT_SPEED=y
#ESP_HOSTED_TRANSPORT_CP_SDIO_STREAMING_MODE=y
# For SPI Full duplex C6 on ESP32-P4-Function-EV-Board, please uncomment below
#CONFIG_ESP_HOST_DEV_BOARD_P4_FUNC_BOARD=y
#CONFIG_ESP_HOSTED_TRANSPORT_CP_SPI=y
# 8. Wifi config
#----------------
CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM=20
CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM=64
CONFIG_ESP_WIFI_DYNAMIC_TX_BUFFER_NUM=64
CONFIG_ESP_WIFI_AMPDU_TX_ENABLED=y
CONFIG_ESP_WIFI_TX_BA_WIN=32
CONFIG_ESP_WIFI_AMPDU_RX_ENABLED=y
CONFIG_ESP_WIFI_RX_BA_WIN=32
CONFIG_ESP_WIFI_RX_IRAM_OPT=y
CONFIG_ESP_WIFI_IRAM_OPT=y
# # 9. LWIP config
# #----------------
# CONFIG_LWIP_TCP_SND_BUF_DEFAULT=65535
# CONFIG_LWIP_TCP_WND_DEFAULT=65535
# CONFIG_LWIP_TCP_RECVMBOX_SIZE=64
# CONFIG_LWIP_UDP_RECVMBOX_SIZE=64
# CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=64
# CONFIG_LWIP_TCP_SACK_OUT=y
# #CONFIG_LWIP_TCPIP_CORE_LOCKING=y
# #CONFIG_LWIP_TCPIP_CORE_LOCKING_INPUT=y
# 10. Optimizations
#-------------------
CONFIG_COMPILER_OPTIMIZATION_PERF=y
# 11. Stats
#-----------
#CONFIG_ESP_HOSTED_FUNCTION_PROFILING=y
#CONFIG_ESP_PKT_STATS=y
# # 12 iperf task
# #--------------