OTA bring-up fixes: loop-task jobs + PSRAM TLS, ESP-NOW survives Wi-Fi, wifi console
Firmware / build (push) Successful in 4m24s
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>
This commit is contained in:
+41
-12
@@ -25,6 +25,8 @@
|
||||
#include <esp_partition.h>
|
||||
#include <mbedtls/sha256.h>
|
||||
#include <mbedtls/pk.h>
|
||||
#include <mbedtls/platform.h>
|
||||
#include <esp_heap_caps.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
// Take over Arduino's rollback hook: by default initArduino() marks a new image
|
||||
@@ -41,6 +43,12 @@ static const uint32_t OTA_CONFIRM_MS = OTA_CONFIRM_S * 1000;
|
||||
static const uint32_t BUS_SILENT_MS = 2000; // no frames for this long = ignition off / bench
|
||||
static const uint32_t STALL_TIMEOUT_MS = 15000; // download aborts if no bytes arrive this long
|
||||
static const size_t CHUNK = 4096;
|
||||
// Jobs run inside the Arduino loop task (8 KB internal stack), not a task of
|
||||
// their own: with the display + Wi-Fi up, internal RAM has no block big enough
|
||||
// for another TLS-capable stack (measured 2026-09-26: 15.9 KB free, largest
|
||||
// block 7.6 KB), and the stack can't go to PSRAM because esp_ota_write()
|
||||
// disables the flash cache. The loop (screen value ticks, console) pauses
|
||||
// while a job runs - acceptable: parked, and an install ends in a reboot.
|
||||
|
||||
typedef struct {
|
||||
char tag[32];
|
||||
@@ -54,7 +62,8 @@ typedef struct {
|
||||
|
||||
enum job_t { JOB_CHECK, JOB_INSTALL, JOB_INSTALL_FORCE };
|
||||
|
||||
static volatile bool g_busy = false;
|
||||
static bool g_job_pending = false;
|
||||
static job_t g_job = JOB_CHECK;
|
||||
static bool g_pending_verify = false;
|
||||
|
||||
// ============================================================================
|
||||
@@ -378,23 +387,38 @@ static void check() {
|
||||
same ? " - up to date" : " - `ota install` to update");
|
||||
}
|
||||
|
||||
static void jobTask(void* arg) {
|
||||
job_t job = (job_t)(intptr_t)arg;
|
||||
// This core's mbedtls is built with CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC: every TLS
|
||||
// session wants ~40 KB of *internal* RAM, which the display + Wi-Fi leave no
|
||||
// room for. Route mbedtls to PSRAM (internal as fallback) while a job runs.
|
||||
// Both allocators free with heap_caps_free(), so swapping them is always safe.
|
||||
static void* tlsCallocPsram(size_t n, size_t size) {
|
||||
return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT,
|
||||
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
}
|
||||
static void* tlsCallocInternal(size_t n, size_t size) {
|
||||
return heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
}
|
||||
|
||||
static void printHeap(const char* when) {
|
||||
print("[ota] heap %s: internal free %u (largest block %u), PSRAM free %u", when,
|
||||
(unsigned)heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT),
|
||||
(unsigned)heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT),
|
||||
(unsigned)heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
|
||||
}
|
||||
|
||||
static void runJob(job_t job) {
|
||||
mbedtls_platform_set_calloc_free(tlsCallocPsram, heap_caps_free);
|
||||
if (job == JOB_CHECK) check();
|
||||
else install(job == JOB_INSTALL_FORCE);
|
||||
g_busy = false;
|
||||
vTaskDelete(NULL);
|
||||
mbedtls_platform_set_calloc_free(tlsCallocInternal, heap_caps_free);
|
||||
print("[ota] loop stack headroom after job: %u B", (unsigned)uxTaskGetStackHighWaterMark(NULL));
|
||||
}
|
||||
|
||||
static void startJob(job_t job) {
|
||||
if (g_busy) { print("[ota] busy - an ota job is already running"); return; }
|
||||
if (!gate(true, job != JOB_CHECK)) return; // checking is harmless; installing needs parked
|
||||
g_busy = true;
|
||||
// TLS needs a deep stack; core 0 alongside the other network tasks.
|
||||
if (xTaskCreatePinnedToCore(jobTask, "ota", 16384, (void*)(intptr_t)job, 1, NULL, 0) != pdPASS) {
|
||||
g_busy = false;
|
||||
print("[ota] could not start the ota task (out of memory?)");
|
||||
}
|
||||
g_job = job;
|
||||
g_job_pending = true; // runs on the next tick(), in the loop task
|
||||
print("[ota] working - console and screen values pause until done");
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -420,6 +444,7 @@ static void status() {
|
||||
}
|
||||
}
|
||||
print("[ota] updates from %s/%s/releases (%s)", OTA_SERVER, OTA_REPO, OTA_TARGET);
|
||||
printHeap("now");
|
||||
}
|
||||
|
||||
static void rollback() {
|
||||
@@ -474,6 +499,10 @@ void init() {
|
||||
}
|
||||
|
||||
void tick() {
|
||||
if (g_job_pending) {
|
||||
g_job_pending = false;
|
||||
runJob(g_job);
|
||||
}
|
||||
if (g_pending_verify && millis() > OTA_CONFIRM_MS) {
|
||||
esp_ota_mark_app_valid_cancel_rollback();
|
||||
g_pending_verify = false;
|
||||
|
||||
Reference in New Issue
Block a user