bolar_2 backup

This commit is contained in:
2026-11-27 16:38:11 +08:00
commit e96d6d19df
305 changed files with 43350 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
#pragma once
#include <cstddef>
namespace borla::task_stack_budget {
// ESP-IDF's FreeRTOS task creation APIs on ESP32-S3 express stack depth in
// bytes. Keep the unit in each name so future changes do not accidentally
// treat these values as host FreeRTOS StackType_t words. The two large audio
// stacks are allocated from PSRAM; connectivity stacks stay in internal RAM.
inline constexpr size_t kPlaybackBytes = 36 * 1024;
// Internal RAM, and dynamically allocated so BLE provisioning gets it back
// when the audio engine is stopped. This is the one audio task with a hard
// deadline: it must finish read + AEC + wake word inside 60 ms or the I2S RX
// DMA overflows and the AEC loses microphone/reference alignment. Stack access
// is the most frequent memory operation in that code, and serving it from PSRAM
// — over a bus the encoder task is simultaneously hammering — was measured at
// 56-70 ms for the DSP stage alone.
//
// The previous firmware ran the identical work (VoiceProcessor + WakeNet) in
// 16 KB of internal RAM; 20 KB here leaves margin for the 1920-byte reference
// frame this implementation keeps on the stack. Watch capture2's
// stack_free_bytes in the diagnostics snapshot before trimming further.
// 16 KB matches the previous firmware's audio_in task for the same workload;
// the measured high-water mark here is ~6.3 KB (capture2 stack_free_bytes =
// 14172 of the earlier 20 KB, read while duplex AEC was active).
inline constexpr size_t kCaptureBytes = 10 * 1024;//16 * 1024;
// Opus encoding only; runs on the other core so it never competes with the
// capture loop's 60 ms frame budget.
//
// Sized from where the work came from, not from the task's idle footprint.
// libopus' SILK path (silk_encode_frame_FIX -> silk_find_pitch_lags_FIX ->
// silk_autocorr -> celt_pitch_xcorr) puts large arrays on the stack; the
// combined capture task that used to do this was measured at ~25 KB in use.
// A first attempt at 16 KB tripped the stack canary inside celt_pitch_xcorr_c
// on the very first encoded frame and put the device in a boot loop. A later
// trim to 24 KB tripped it again deeper in the same path (stack canary in
// silk_NSQ_del_dec_c at NSQ_del_dec.c:266, the call into
// silk_noise_shape_quantizer_del_dec, 2026-09-23): the panic backtrace spans
// 0x3c842cb0..0x3c848b60, i.e. the opus_encode() chain alone walked 24,240 of
// the 24,576 budgeted bytes before the next frame crossed the limit. This is
// PSRAM, so the headroom costs no internal RAM.
inline constexpr size_t kCaptureEncoderBytes = 32 * 1024;
// Sized from measured high-water marks after a full TLS + OTA + provisioning
// cycle, not guessed. Every byte here is .bss, i.e. it comes straight off the
// front of the main internal heap region — and that region is what BLE
// provisioning runs out of when it has to re-init the WiFi driver alongside the
// BT controller. Field numbers at the time of sizing (used / previous budget):
// connectivity2 2492 / 8192 dual_network2 3136 / 8192
// friend_service2 960 / 4096 alarm2 1172 / 4096
inline constexpr size_t kConnectivityManagerBytes = 5 * 1024;//6 * 1024;
inline constexpr size_t kDualNetworkBytes = 4 * 1024;//5 * 1024;
inline constexpr size_t kWebsocketTransportBytes = 10 * 1024;//12 * 1024;
inline constexpr size_t kFriendServiceBytes = 3 * 1024;
inline constexpr size_t kAlarmServiceBytes = 3 * 1024;
inline constexpr size_t kRuntimeDiagnosticsBytes = 8 * 1024;
inline constexpr size_t kLocalPromptProducerBytes = 6 * 1024;
// BLE provisioning worker: carries esp_wifi_scan_start, nimble_port_init and
// NVS credential writes on one stack, so it cannot live in PSRAM (NVS runs
// with the cache disabled) and needs real headroom.
inline constexpr size_t kBleProvisioningBytes = 8 * 1024;
} // namespace borla::task_stack_budget
namespace borla::task_priority {
// The audio path is a real-time system and its priorities are load-bearing,
// not incidental. Layout adopted from the previous firmware, which ran the
// same DSP on the same silicon without stuttering (audio_service.cc:56-58):
//
// audio_in prio 8 core 0 capture + AEC + wake word
// audio_out prio 4 core 1 I2S write
// opus_codec prio 2 core 1 encode + decode
//
// The rule is that the two tasks with a hard 60 ms deadline must preempt the
// CPU-heavy one, never the reverse. This firmware originally ran all three at
// priority 5; a single Opus encode costs ~50 ms of CPU, so at equal priority it
// simply took the speaker task's slot and playback stuttered audibly.
//
// The speaker task is 6 rather than the previous firmware's 4 because it is
// not the same task. The old audio_out was a pure I2S writer — per wake-up it
// memcpy'd already-decoded PCM into the DMA, work so small that time-slicing
// against the priority-4 service crowd could not hurt it, and behind it stood
// an 8-frame decoded-PCM queue (480 ms of runway) filled by the codec task.
// Here decode+write are fused into one task whose only runway is the I2S DMA
// itself, so a 10-15 ms Opus decode delayed by round-robin against the
// UNPINNED priority-4/5 tasks (websocket2 doing TLS at 4, imu_sensor2 doing
// I2C at 5, touch/battery/alarm/reminder at 4 — all free to land on core 1)
// occasionally missed the deadline: the field symptom was intermittent
// playback stutter. 6 puts the speaker above that crowd and below capture.
// Priority inheritance covers the shared mutexes (ESP-IDF std::mutex is a
// FreeRTOS PI mutex underneath).
inline constexpr unsigned kCaptureLoop = 8;
inline constexpr unsigned kPlaybackLoop = 6;
inline constexpr unsigned kAudioCodec = 2;
} // namespace borla::task_priority