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>
315 lines
10 KiB
C
315 lines
10 KiB
C
#include "espnow_tx.h"
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "esp_check.h"
|
|
#include "esp_event.h"
|
|
#include "esp_log.h"
|
|
#include "esp_rom_sys.h"
|
|
#include "esp_random.h"
|
|
#include "esp_timer.h"
|
|
#include "bench.h"
|
|
#include "bridge.h"
|
|
#if !CONFIG_IDF_TARGET_LINUX
|
|
#include "esp_netif.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_mac.h"
|
|
#include "esp_wifi_ap_get_sta_list.h"
|
|
#endif
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/queue.h"
|
|
#include "freertos/semphr.h"
|
|
#include "freertos/task.h"
|
|
|
|
#define BULK_MAX 250
|
|
#define BULK_RETRIES 3
|
|
#define BULK_QUEUE_LEN 32
|
|
|
|
static const char *TAG = "espnow_tx";
|
|
static const uint8_t BROADCAST[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
|
|
|
|
typedef struct {
|
|
uint8_t mac[6];
|
|
bool broadcast;
|
|
uint8_t len;
|
|
uint8_t data[BULK_MAX];
|
|
} bulk_t;
|
|
|
|
static QueueHandle_t s_bulk_q;
|
|
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 espnow_stats_t s_stats;
|
|
static volatile int s_loss_pct;
|
|
static volatile bool s_loss_beats = true;
|
|
static espnow_rx_cb_t s_rx_cb;
|
|
static char s_bridges[BRIDGE_MAX_LINKS][BRIDGE_ADDR_MAX]; // the bridges to open at init
|
|
static size_t s_nbridges;
|
|
|
|
void espnow_tx_set_bridges(const char addrs[][BRIDGE_ADDR_MAX], size_t n)
|
|
{
|
|
if (n > BRIDGE_MAX_LINKS) {
|
|
n = BRIDGE_MAX_LINKS;
|
|
}
|
|
memcpy(s_bridges, addrs, n * sizeof(s_bridges[0]));
|
|
s_nbridges = n;
|
|
}
|
|
|
|
// 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)
|
|
{
|
|
return bridge_active() ? bridge_send(dst, data, len) : ESP_ERR_INVALID_STATE;
|
|
}
|
|
|
|
bool espnow_tx_drop(void)
|
|
{
|
|
return s_loss_pct > 0 && (int)(esp_random() % 100) < s_loss_pct;
|
|
}
|
|
|
|
void espnow_tx_set_loss(int percent, bool include_beats)
|
|
{
|
|
s_loss_pct = percent < 0 ? 0 : (percent > 100 ? 100 : percent);
|
|
s_loss_beats = include_beats;
|
|
}
|
|
|
|
int espnow_tx_loss(bool *include_beats)
|
|
{
|
|
if (include_beats) {
|
|
*include_beats = s_loss_beats;
|
|
}
|
|
return s_loss_pct;
|
|
}
|
|
|
|
static void bridge_rx(const uint8_t *src, const uint8_t *data, int len)
|
|
{
|
|
if (s_rx_cb) {
|
|
s_rx_cb(src, data, len);
|
|
}
|
|
}
|
|
|
|
esp_err_t espnow_tx_set_rx_cb(espnow_rx_cb_t cb)
|
|
{
|
|
s_rx_cb = cb;
|
|
return ESP_OK;
|
|
}
|
|
|
|
// Each copy carries a fire_in_ms reduced by the time already spent, so every
|
|
// copy points at the same instant.
|
|
static void send_beat_copy(void *arg)
|
|
{
|
|
(void)arg;
|
|
int sent = CONFIG_BEAT_TX_COPIES - s_copies_left;
|
|
int gap_ms = (sent * CONFIG_BEAT_TX_GAP_US) / 1000;
|
|
int fire = CONFIG_BEAT_LEAD_MS - gap_ms;
|
|
s_pending.fire_in_ms = fire < 0 ? 0 : (fire > 255 ? 255 : fire);
|
|
|
|
if (s_loss_beats && espnow_tx_drop()) {
|
|
s_stats.sim_dropped++; // lossy-link test: this copy never reaches the radio
|
|
} else if (radio_send(BROADCAST, &s_pending, sizeof(s_pending)) != ESP_OK) {
|
|
s_stats.beat_send_fail++;
|
|
}
|
|
s_last_beat_us = esp_timer_get_time();
|
|
if (--s_copies_left > 0) {
|
|
esp_timer_start_once(s_copy_timer, CONFIG_BEAT_TX_GAP_US);
|
|
}
|
|
}
|
|
|
|
void espnow_tx_beat(float bpm, bool downbeat, int bar)
|
|
{
|
|
if (bpm < 30.0f) {
|
|
return;
|
|
}
|
|
esp_timer_stop(s_copy_timer); // a still-pending copy belongs to the previous beat
|
|
s_pending.magic = BEAT_FRAME_MAGIC;
|
|
s_pending.type = BEAT_FRAME_TYPE;
|
|
s_pending.flags = (downbeat ? BEAT_FLAG_DOWNBEAT : 0) | ((bar & 7) << BEAT_FLAG_BAR_SHIFT);
|
|
s_pending.beat_idx = ++s_beat_idx;
|
|
s_pending.period_ms = (uint16_t)(60000.0f / bpm);
|
|
s_copies_left = CONFIG_BEAT_TX_COPIES;
|
|
s_stats.beats_sent++;
|
|
bench_on_beat_tx(s_pending.beat_idx, esp_timer_get_time(), CONFIG_BEAT_LEAD_MS);
|
|
send_beat_copy(NULL);
|
|
}
|
|
|
|
static volatile bool s_mic_beats = true;
|
|
|
|
void espnow_tx_set_mic_beats(bool on)
|
|
{
|
|
s_mic_beats = on;
|
|
}
|
|
|
|
// Runs in the audio task straight from the detector: no queue, no allocation.
|
|
static void on_beat(const beat_info_t *info, float bpm, bool downbeat)
|
|
{
|
|
if (!s_mic_beats) {
|
|
return;
|
|
}
|
|
if (bench_running()) {
|
|
return; // the bench generator owns the beat stream while it runs
|
|
}
|
|
espnow_tx_beat(bpm, downbeat, info->bar_sure ? info->bar_beat : 0);
|
|
}
|
|
|
|
static void bulk_task(void *arg)
|
|
{
|
|
bulk_t b;
|
|
for (;;) {
|
|
xQueueReceive(s_bulk_q, &b, portMAX_DELAY);
|
|
|
|
// Keep the channel clear around beats
|
|
int64_t wait_us = CONFIG_BULK_HOLDOFF_US - (esp_timer_get_time() - s_last_beat_us);
|
|
if (wait_us > 0) {
|
|
esp_rom_delay_us(wait_us);
|
|
}
|
|
|
|
const uint8_t *dst = b.broadcast ? BROADCAST : b.mac;
|
|
int tries = b.broadcast ? 1 : BULK_RETRIES;
|
|
bool ok = false;
|
|
for (int t = 0; t < tries && !ok; t++) {
|
|
if (t) {
|
|
s_stats.bulk_retries++;
|
|
vTaskDelay(pdMS_TO_TICKS(5 << t));
|
|
}
|
|
if (espnow_tx_drop()) {
|
|
s_stats.sim_dropped++; // lossy-link test: pretend the radio sent it
|
|
ok = true;
|
|
} else {
|
|
ok = radio_send(dst, b.data, b.len) == ESP_OK; // no MAC-layer ack: rel.c's acks retry
|
|
}
|
|
}
|
|
s_stats.bulk_sent += ok;
|
|
s_stats.bulk_failed += !ok;
|
|
vTaskDelay(pdMS_TO_TICKS(3)); // inter-frame gap
|
|
}
|
|
}
|
|
|
|
esp_err_t espnow_tx_send(const uint8_t *mac, const void *data, size_t len)
|
|
{
|
|
if (len == 0 || len > BULK_MAX) {
|
|
return ESP_ERR_INVALID_SIZE;
|
|
}
|
|
bulk_t b = { .broadcast = mac == NULL, .len = len };
|
|
if (mac) {
|
|
memcpy(b.mac, mac, 6);
|
|
}
|
|
memcpy(b.data, data, len);
|
|
return xQueueSend(s_bulk_q, &b, 0) == pdTRUE ? ESP_OK : ESP_ERR_NO_MEM;
|
|
}
|
|
|
|
void espnow_tx_stats(espnow_stats_t *out)
|
|
{
|
|
*out = s_stats;
|
|
}
|
|
|
|
#if !CONFIG_IDF_TARGET_LINUX
|
|
// A link-local IPv6 address on the AP lets the mDNS responder answer the AAAA half of a browser's
|
|
// lookup; without it the browser waits about a second for an answer that never comes.
|
|
static esp_netif_t *s_ap_netif;
|
|
static void ap_event(void *arg, esp_event_base_t base, int32_t id, void *data)
|
|
{
|
|
if (s_ap_netif) {
|
|
esp_netif_create_ip6_linklocal(s_ap_netif);
|
|
}
|
|
}
|
|
|
|
int espnow_tx_ap_clients(char *out, size_t n)
|
|
{
|
|
int len = snprintf(out, n, "[");
|
|
wifi_sta_list_t sl;
|
|
wifi_sta_mac_ip_list_t ml;
|
|
if (s_ap_netif && esp_wifi_ap_get_sta_list(&sl) == ESP_OK && esp_wifi_ap_get_sta_list_with_ip(&sl, &ml) == ESP_OK) {
|
|
for (int i = 0; i < ml.num && len < (int)n - 64; i++) {
|
|
len += snprintf(out + len, n - len, "%s{\"mac\":\"" MACSTR "\",\"ip\":\"" IPSTR "\"}", i ? "," : "",
|
|
MAC2STR(ml.sta[i].mac), IP2STR(&ml.sta[i].ip));
|
|
}
|
|
}
|
|
len += snprintf(out + len, n - len, "]");
|
|
return len;
|
|
}
|
|
|
|
static esp_err_t radio_init(const char *ap_ssid, const char *ap_password)
|
|
{
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
ESP_RETURN_ON_ERROR(esp_wifi_init(&cfg), TAG, "wifi init");
|
|
ESP_RETURN_ON_ERROR(esp_wifi_set_storage(WIFI_STORAGE_RAM), TAG, "storage");
|
|
if (ap_ssid) {
|
|
// Not the Ethernet fallback subnet (192.168.4.x)
|
|
esp_netif_t *ap = esp_netif_create_default_wifi_ap();
|
|
ESP_RETURN_ON_FALSE(ap, ESP_FAIL, TAG, "ap netif");
|
|
s_ap_netif = ap;
|
|
ESP_RETURN_ON_ERROR(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_START, ap_event, NULL), TAG, "ap event");
|
|
esp_netif_ip_info_t ip = { 0 };
|
|
ip.ip.addr = ESP_IP4TOADDR(192, 168, 5, 1);
|
|
ip.gw.addr = ip.ip.addr;
|
|
ip.netmask.addr = ESP_IP4TOADDR(255, 255, 255, 0);
|
|
esp_netif_dhcps_stop(ap);
|
|
ESP_RETURN_ON_ERROR(esp_netif_set_ip_info(ap, &ip), TAG, "ap ip");
|
|
esp_netif_dhcps_start(ap);
|
|
wifi_config_t ac = { 0 };
|
|
strlcpy((char *)ac.ap.ssid, ap_ssid, sizeof(ac.ap.ssid));
|
|
ac.ap.ssid_len = strlen((char *)ac.ap.ssid);
|
|
ac.ap.channel = CONFIG_ESPNOW_CHANNEL;
|
|
if (ap_password && strlen(ap_password) >= 8) {
|
|
strlcpy((char *)ac.ap.password, ap_password, sizeof(ac.ap.password));
|
|
ac.ap.authmode = WIFI_AUTH_WPA2_PSK;
|
|
} else {
|
|
ac.ap.authmode = WIFI_AUTH_OPEN;
|
|
}
|
|
ac.ap.max_connection = 4;
|
|
ESP_RETURN_ON_ERROR(esp_wifi_set_mode(WIFI_MODE_APSTA), TAG, "mode");
|
|
ESP_RETURN_ON_ERROR(esp_wifi_set_config(WIFI_IF_AP, &ac), TAG, "ap config");
|
|
} else {
|
|
ESP_RETURN_ON_ERROR(esp_wifi_set_mode(WIFI_MODE_STA), TAG, "mode");
|
|
}
|
|
ESP_RETURN_ON_ERROR(esp_wifi_start(), TAG, "start");
|
|
ESP_RETURN_ON_ERROR(esp_wifi_set_ps(WIFI_PS_NONE), TAG, "ps");
|
|
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");
|
|
|
|
// 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
|
|
|
|
#if CONFIG_IDF_TARGET_LINUX
|
|
int espnow_tx_ap_clients(char *out, size_t n)
|
|
{
|
|
return snprintf(out, n, "[]");
|
|
}
|
|
#endif
|
|
|
|
esp_err_t espnow_tx_change_bridges(const char addrs[][BRIDGE_ADDR_MAX], size_t n)
|
|
{
|
|
espnow_tx_set_bridges(addrs, n);
|
|
return bridge_set_all(addrs, n, bridge_rx);
|
|
}
|
|
|
|
esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
|
{
|
|
#if CONFIG_IDF_TARGET_LINUX
|
|
(void)ap_ssid;
|
|
(void)ap_password;
|
|
ESP_RETURN_ON_FALSE(s_nbridges, ESP_ERR_INVALID_STATE, TAG, "the host build needs a bridge address");
|
|
#else
|
|
// 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));
|
|
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");
|
|
xTaskCreate(bulk_task, "espnow_bulk", 4096, NULL, 5, NULL);
|
|
|
|
beat_set_callback(on_beat);
|
|
return ESP_OK;
|
|
}
|