Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions firmware/esp32-csi-node/main/c6_sync_espnow.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
#include "esp_idf_version.h"
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "nvs_config.h"
#include <string.h>

static const char *TAG = "c6_espnow";

extern nvs_config_t g_nvs_config;

#define BEACON_MAGIC 0x53454E50u /* 'SENP' little-endian */
#define BEACON_PROTO_VER 0x01
#define BEACON_PERIOD_MS 100
Expand All @@ -43,6 +46,26 @@ typedef struct __attribute__((packed)) {

static const uint8_t s_broadcast_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

/* Send beacons at 6 Mbps OFDM, not ESP-NOW's 1 Mbps 802.11b default. When
* the STA's 11b rates are disabled (the default, see nvs_config.h)
* a 1 Mbps beacon would not be sendable at all; and OFDM carries the LTF that
* CSI is computed from, at about a quarter of the airtime. With 11b allowed
* (NVS allow_11b=1) the old default is left untouched. */
static void espnow_set_beacon_rate(void)
{
if (g_nvs_config.wifi_allow_11b) return;
esp_now_rate_config_t rc = {
.phymode = WIFI_PHY_MODE_11G,
.rate = WIFI_PHY_RATE_6M,
.ersu = false,
.dcm = false,
};
esp_err_t r = esp_now_set_peer_rate_config(s_broadcast_mac, &rc);
if (r != ESP_OK) {
ESP_LOGW(TAG, "beacon rate 6M OFDM not set: %s", esp_err_to_name(r));
}
}

static uint64_t s_local_id = 0; /* 6-byte MAC packed into u64 */
static uint64_t s_leader_id = 0;
static int64_t s_offset_us = 0;
Expand Down Expand Up @@ -212,6 +235,7 @@ esp_err_t c6_sync_espnow_init(void)
if (r != ESP_OK && r != ESP_ERR_ESPNOW_EXIST) {
ESP_LOGW(TAG, "esp_now_add_peer(broadcast) failed: %s", esp_err_to_name(r));
}
espnow_set_beacon_rate();

/* Start as candidate leader — will step down on receiving lower-id beacon. */
s_is_leader = true;
Expand Down
11 changes: 11 additions & 0 deletions firmware/esp32-csi-node/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ static void wifi_init_sta(void)
s_reconnect_timer = NULL;
}

/* Keep rate control out of 802.11b (1-11 Mbps DSSS/CCK). It must be set
* after esp_wifi_init() and before esp_wifi_start(). Non-fatal: if the
* driver refuses, the node runs as before rather than not at all. */
if (!g_nvs_config.wifi_allow_11b) {
esp_err_t r11b = esp_wifi_config_11b_rate(WIFI_IF_STA, true);
ESP_LOGI(TAG, "802.11b rates on STA: %s (%s)",
r11b == ESP_OK ? "disabled" : "STILL ENABLED", esp_err_to_name(r11b));
} else {
ESP_LOGW(TAG, "802.11b rates on STA: allowed (NVS allow_11b=1)");
}

ESP_ERROR_CHECK(esp_wifi_start());

ESP_LOGI(TAG, "WiFi STA initialized, connecting to SSID: %s", g_nvs_config.wifi_ssid);
Expand Down
12 changes: 12 additions & 0 deletions firmware/esp32-csi-node/main/nvs_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void nvs_config_load(nvs_config_t *cfg)
cfg->dwell_ms = 50;
cfg->tdm_slot_index = 0;
cfg->tdm_node_count = 1;
cfg->wifi_allow_11b = 0; /* 802.11b rates off by default */

/* ADR-039: Edge intelligence defaults from Kconfig. */
#ifdef CONFIG_EDGE_TIER
Expand Down Expand Up @@ -192,6 +193,17 @@ void nvs_config_load(nvs_config_t *cfg)
}
}

/* 802.11b rate-control override (see nvs_config.h). */
uint8_t allow_11b_val;
if (nvs_get_u8(handle, "allow_11b", &allow_11b_val) == ESP_OK) {
if (allow_11b_val <= 1) {
cfg->wifi_allow_11b = allow_11b_val;
ESP_LOGI(TAG, "NVS override: wifi_allow_11b=%u", (unsigned)allow_11b_val);
} else {
ESP_LOGW(TAG, "NVS allow_11b=%u not 0/1, ignored", (unsigned)allow_11b_val);
}
}

/* ADR-039: Edge intelligence overrides. */
uint8_t edge_tier_val;
if (nvs_get_u8(handle, "edge_tier", &edge_tier_val) == ESP_OK) {
Expand Down
13 changes: 13 additions & 0 deletions firmware/esp32-csi-node/main/nvs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ typedef struct {
uint8_t tdm_slot_index; /**< This node's TDM slot index (0-based). */
uint8_t tdm_node_count; /**< Total nodes in the TDM schedule. */

/* 802.11b rates on the STA interface (NVS key "allow_11b").
*
* 0 (default) = disabled: rate control cannot fall below 6 Mbps OFDM.
* With 11b allowed, a node whose link degrades can settle at 1-2 Mbps
* and stay there; each CSI datagram then takes tens of times the
* airtime, the whole fleet's channel fills up and frame rates slide.
* The TX queue also backs up, which shows as deep free-heap dips and
* send_fail bursts that look like a leak. On a 9-node ESP32-C6 fleet,
* disabling 11b took the channel from 80-85% busy to ~44%.
* 1 = allow (the previous behaviour), kept as a no-reflash rollback;
* needs a reboot, since it is applied before esp_wifi_start(). */
uint8_t wifi_allow_11b;

/* ADR-039: Edge intelligence configuration */
uint8_t edge_tier; /**< Processing tier (0=raw, 1=basic, 2=full). */
float presence_thresh; /**< Presence threshold (0 = auto-calibrate). */
Expand Down
Loading