bb0895674a
Firmware / build (push) Successful in 4m24s
- ota: internal RAM has 15.9 KB free / 7.6 KB largest block with Wi-Fi up, so jobs now run in the loop task (8 KB stack; 3.6 KB headroom measured on `ota check`) with mbedtls allocations routed to PSRAM during the job - wifi_manager: WiFi.disconnect(true) powered the radio off and killed ESP-NOW whenever Wi-Fi was used; now disconnect() only. showConnectionResult() waits for the LVGL lock and null-checks widgets. New `wifi join|status|off` console - espnow_link: send() re-initialises ESP-NOW if the driver dropped it - wiki: ota-updates memory + bugs sections, open-questions #13 (internal RAM), log; CLAUDE.md notes Verified on the board: `wifi join` connects, ESP-NOW 195 sent / 0 failed while joined, `ota check` verifies v0.1.0's signature on-device. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
2.8 KiB
C++
97 lines
2.8 KiB
C++
/*
|
|
* ESP-NOW Link Module Implementation (Controller A, transmit only)
|
|
*/
|
|
|
|
#include "espnow_link.h"
|
|
#include "protocol/automotive_data.h"
|
|
#include <WiFi.h>
|
|
#include <esp_wifi.h>
|
|
#include <esp_now.h>
|
|
#include "esp_log.h"
|
|
|
|
namespace EspNowLink {
|
|
|
|
static const uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
|
|
|
static bool g_ready = false;
|
|
static uint32_t g_sent = 0;
|
|
static uint32_t g_fail = 0;
|
|
|
|
// IDF 5.5 (pioarduino / Arduino 3.3) passes wifi_tx_info_t*; older cores passed the MAC.
|
|
static void onSent(const wifi_tx_info_t* info, esp_now_send_status_t status) {
|
|
(void)info;
|
|
// Broadcast frames are never ACKed, so "success" only means the radio
|
|
// transmitted. Failures here mean the radio itself refused the frame.
|
|
if (status != ESP_NOW_SEND_SUCCESS) g_fail++;
|
|
}
|
|
|
|
bool init() {
|
|
if (g_ready) return true;
|
|
|
|
// Radio must be up for ESP-NOW. Keep STA regardless of the WiFi toggle.
|
|
if (WiFi.getMode() == WIFI_OFF) {
|
|
WiFi.mode(WIFI_STA);
|
|
}
|
|
WiFi.setSleep(false); // modem sleep adds latency/loss to ESP-NOW
|
|
|
|
// Fixed channel while not associated. If the WiFi manager later joins an
|
|
// AP the channel follows the AP; B's receiver hops to find us.
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
esp_wifi_set_channel(ESPNOW_CHANNEL, WIFI_SECOND_CHAN_NONE);
|
|
}
|
|
|
|
if (esp_now_init() != ESP_OK) {
|
|
ESP_LOGE("EspNow", "esp_now_init failed");
|
|
return false;
|
|
}
|
|
esp_now_register_send_cb(onSent);
|
|
|
|
esp_now_peer_info_t peer = {};
|
|
memcpy(peer.peer_addr, BROADCAST_MAC, 6);
|
|
peer.channel = 0; // current channel
|
|
peer.encrypt = false;
|
|
peer.ifidx = WIFI_IF_STA;
|
|
if (esp_now_add_peer(&peer) != ESP_OK) {
|
|
ESP_LOGE("EspNow", "esp_now_add_peer(broadcast) failed");
|
|
return false;
|
|
}
|
|
|
|
g_ready = true;
|
|
ESP_LOGI("EspNow", "ESP-NOW ready: STA %s, channel %d, broadcast peer added",
|
|
WiFi.macAddress().c_str(), WiFi.channel());
|
|
return true;
|
|
}
|
|
|
|
// Anything that restarts the Wi-Fi driver drops ESP-NOW with it. Re-init at
|
|
// most once a second instead of failing every frame from then on.
|
|
static void reinitIfDue() {
|
|
static uint32_t last = 0;
|
|
if (millis() - last < 1000) return;
|
|
last = millis();
|
|
g_ready = false;
|
|
ESP_LOGW("EspNow", "ESP-NOW not initialised - re-initialising");
|
|
init();
|
|
}
|
|
|
|
bool send(const uint8_t* frame, size_t len) {
|
|
if (len == 0 || len > ESP_NOW_MAX_DATA_LEN) return false;
|
|
if (!g_ready) {
|
|
reinitIfDue();
|
|
if (!g_ready) return false;
|
|
}
|
|
esp_err_t err = esp_now_send(BROADCAST_MAC, frame, len);
|
|
if (err == ESP_OK) {
|
|
g_sent++;
|
|
return true;
|
|
}
|
|
g_fail++;
|
|
if (err == ESP_ERR_ESPNOW_NOT_INIT) reinitIfDue();
|
|
return false;
|
|
}
|
|
|
|
bool isReady() { return g_ready; }
|
|
uint32_t sentCount() { return g_sent; }
|
|
uint32_t failCount() { return g_fail; }
|
|
|
|
} // namespace EspNowLink
|