@geastack/targets 0.1.100 → 0.1.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/package.json +7 -7
  2. package/targets/cmake/geatsc-generated-archive-build.mjs +32 -1
  3. package/targets/esp32/app_project.cmake +2 -2
  4. package/targets/esp32/chip_bindings/audio/echo_cancellation.cpp +392 -28
  5. package/targets/esp32/chip_bindings/audio/echo_cancellation.h +18 -2
  6. package/targets/esp32/chip_bindings/audio/es8311.cpp +117 -13
  7. package/targets/esp32/chip_bindings/displays/co5300.cpp +68 -28
  8. package/targets/esp32/components/gea_framework/CMakeLists.txt +17 -1
  9. package/targets/esp32/components/gea_framework/idf_component.yml +4 -0
  10. package/targets/esp32/components/gea_vpx/CMakeLists.txt +1 -1
  11. package/targets/esp32/connectivity/wifi.cpp +800 -543
  12. package/targets/esp32/display.cpp +121 -26
  13. package/targets/esp32/gea_framework.cmake +22 -28
  14. package/targets/esp32/host_sources.cmake +1 -0
  15. package/targets/esp32/services/comparison_benchmark.h +137 -0
  16. package/targets/esp32/services/comparison_gesture.h +133 -0
  17. package/targets/esp32/services/comparison_input_trace.h +64 -0
  18. package/targets/esp32/services/comparison_upload_capture.h +125 -0
  19. package/targets/esp32/services/debugger.h +634 -0
  20. package/targets/esp32/services/device_control.cpp +965 -20
  21. package/targets/esp32/services/frame_scheduler.cpp +128 -4
  22. package/targets/esp32/services/heap.cpp +125 -0
  23. package/targets/esp32/services/opus_benchmark.h +1 -1
  24. package/targets/esp32/services/storage_service.cpp +119 -104
  25. package/targets/esp32-p4-m5stack-tab5/dependencies.lock +27 -16
  26. package/targets/esp32-s3-m5stack-stopwatch/.clang-format +10 -0
  27. package/targets/esp32-s3-m5stack-stopwatch/README.md +14 -9
  28. package/targets/esp32-s3-m5stack-stopwatch/dependencies.lock +54 -1
  29. package/targets/esp32-s3-m5stack-stopwatch/main/CMakeLists.txt +10 -7
  30. package/targets/esp32-s3-m5stack-stopwatch/main/buttons.cpp +80 -0
  31. package/targets/esp32-s3-m5stack-stopwatch/main/idf_component.yml +18 -0
  32. package/targets/esp32-s3-m5stack-stopwatch/main/include/board.h +38 -17
  33. package/targets/esp32-s3-m5stack-stopwatch/main/panel_reset.cpp +132 -129
  34. package/targets/esp32-s3-m5stack-stopwatch/main/power.cpp +253 -0
  35. package/targets/esp32-s3-m5stack-stopwatch/main/touch.cpp +158 -88
  36. package/targets/esp32-s3-m5stack-stopwatch/sdkconfig.defaults +13 -0
  37. package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +30 -3
  38. package/targets/esp32-s3-touch-amoled-2.06/dependencies.lock +10 -148
  39. package/targets/esp32-s31-espressif-mosaico/CMakeLists.txt +8 -0
  40. package/targets/esp32-s31-espressif-mosaico/README.md +54 -2
  41. package/targets/esp32-s31-espressif-mosaico/dependencies.lock +405 -1
  42. package/targets/esp32-s31-espressif-mosaico/main/CMakeLists.txt +31 -1
  43. package/targets/esp32-s31-espressif-mosaico/main/board_power.cpp +117 -0
  44. package/targets/esp32-s31-espressif-mosaico/main/camera.cpp +389 -0
  45. package/targets/esp32-s31-espressif-mosaico/main/idf_component.yml +17 -3
  46. package/targets/esp32-s31-espressif-mosaico/main/include/board.h +23 -0
  47. package/targets/esp32-s31-espressif-mosaico/main/nand_mount.cpp +172 -0
  48. package/targets/esp32-s31-espressif-mosaico/partitions.csv +7 -3
  49. package/targets/esp32-s31-espressif-mosaico/sdkconfig.defaults +24 -0
@@ -1,4 +1,9 @@
1
1
  #include "services/frame_scheduler.h"
2
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
3
+ #ifndef GEA_NATIVE_DEBUGGER_FPS
4
+ #define GEA_NATIVE_DEBUGGER_FPS 0
5
+ #endif
6
+ #endif
2
7
 
3
8
  #include "app.h"
4
9
  #include "display.h"
@@ -8,9 +13,11 @@
8
13
  #include "ui/refresh_perf.h"
9
14
  #include "ui/document.h"
10
15
  #include "memory_config.h"
16
+ #include "services/comparison_benchmark.h"
11
17
  #include "services/app_state.h"
12
18
  #include "ui/canvas_element.h"
13
19
 
20
+ #include <algorithm>
14
21
  #include <atomic>
15
22
  #include <cstdint>
16
23
  #include <limits>
@@ -23,6 +30,12 @@
23
30
  #include "freertos/task.h"
24
31
  #include "freertos/idf_additions.h"
25
32
 
33
+ #if GEA_EMBEDDED_COMPARISON_BENCHMARK
34
+ #include <sys/time.h>
35
+ extern "C" std::uint32_t gea_display_completed_chunks() __attribute__((weak));
36
+ extern "C" bool gea_display_wait_for_uploads() __attribute__((weak));
37
+ #endif
38
+
26
39
  #if defined(GEA_EMBEDDED_FRAME_BENCHMARK) && GEA_EMBEDDED_FRAME_BENCHMARK
27
40
  extern "C" void gea_frame_benchmark_sample(int64_t start, int64_t done);
28
41
  #if GEA_EMBEDDED_FRAME_BENCHMARK == 2
@@ -247,6 +260,14 @@ public:
247
260
  void setVsyncDriven(bool driven) { vsyncDriven_.store(driven, std::memory_order_release); }
248
261
  bool vsyncDriven() const { return vsyncDriven_.load(std::memory_order_acquire); }
249
262
 
263
+ #if GEA_EMBEDDED_COMPARISON_BENCHMARK
264
+ bool diagnosticCaptureBoundaryReady() const
265
+ {
266
+ const auto stage = frameStage();
267
+ return stage == FrameStage::Idle || stage == FrameStage::WaitAppLock;
268
+ }
269
+ #endif
270
+
250
271
  // Called from the panel's TE (VBlank) edge ISR when TE-sync is enabled: this is the
251
272
  // SOLE frame producer in that mode. Posts one Frame event per edge, coalesced — if a
252
273
  // frame is still queued or running (overrun), the edge is dropped rather than piling
@@ -256,6 +277,9 @@ public:
256
277
  if (!eventQueue_)
257
278
  return;
258
279
  lastVsyncPostUs_.store(static_cast<uint32_t>(esp_timer_get_time()), std::memory_order_release);
280
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
281
+ if (!debuggerFrameDue(static_cast<uint32_t>(esp_timer_get_time()))) return;
282
+ #endif
259
283
  if (eventPending_.load(std::memory_order_acquire))
260
284
  return;
261
285
  if (frameInProgress_.load(std::memory_order_acquire))
@@ -344,6 +368,23 @@ public:
344
368
  // frameStartUs is FUNCTIONAL (drives lastFrameStartUs_, frameStartedMs_, and
345
369
  // the watchdog), so it stays. Only the perf period accumulation is gated.
346
370
  const int64_t frameStartUs = esp_timer_get_time();
371
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
372
+ if (!debuggerFrameDue(static_cast<uint32_t>(frameStartUs))) {
373
+ eventPending_.store(false, std::memory_order_release);
374
+ catchUpRequest_.store(false, std::memory_order_release);
375
+ return;
376
+ }
377
+ scheduleDebuggerFrame(static_cast<uint32_t>(frameStartUs));
378
+ #endif
379
+ #if GEA_EMBEDDED_COMPARISON_BENCHMARK
380
+ const auto comparisonToken = gea::platform::comparison::frameBegin();
381
+ std::uint32_t comparisonCalls = 0, comparisonChunks = 0;
382
+ std::uint64_t comparisonPixels = 0;
383
+ if (comparisonToken.active) {
384
+ gea::platform::display::Display::flushOdometerRead(comparisonCalls, comparisonPixels);
385
+ comparisonChunks = gea_display_completed_chunks ? gea_display_completed_chunks() : 0;
386
+ }
387
+ #endif
347
388
  #if GEA_EMBEDDED_FRAME_SCHEDULER_PERF_LOG
348
389
  if (lastFrameStartUs_ > 0) {
349
390
  perf_.framePeriodUs += frameStartUs - lastFrameStartUs_;
@@ -378,6 +419,13 @@ public:
378
419
  setFrameStage(FrameStage::WaitAppLock);
379
420
  AppState::lock();
380
421
  setFrameStage(FrameStage::AppFrame);
422
+ #if GEA_EMBEDDED_COMPARISON_BENCHMARK
423
+ const auto frozenEpoch = gea::platform::comparison::frozenEpochSeconds.load(std::memory_order_relaxed);
424
+ if (frozenEpoch > 0) {
425
+ const timeval frozenTime{static_cast<time_t>(frozenEpoch), 0};
426
+ settimeofday(&frozenTime, nullptr);
427
+ }
428
+ #endif
381
429
  #if GEA_EMBEDDED_FRAME_SCHEDULER_PERF_LOG
382
430
  const int64_t appStartUs = esp_timer_get_time();
383
431
  #endif
@@ -588,9 +636,29 @@ public:
588
636
  #endif // GEA_EMBEDDED_FRAME_SCHEDULER_PERF_LOG (perf harvest R3 trail)
589
637
 
590
638
  setFrameStage(FrameStage::FrameDone);
639
+ #if GEA_EMBEDDED_COMPARISON_BENCHMARK
640
+ std::uint64_t comparisonCompletionWaitUs = 0;
641
+ bool comparisonCompletionSucceeded = true;
642
+ if (comparisonToken.active && gea::platform::comparison::completionFence.load(std::memory_order_relaxed)) {
643
+ const auto waitStart = esp_timer_get_time();
644
+ comparisonCompletionSucceeded = gea_display_wait_for_uploads && gea_display_wait_for_uploads();
645
+ comparisonCompletionWaitUs = esp_timer_get_time() - waitStart;
646
+ }
647
+ #endif
591
648
  frameInProgress_.store(false, std::memory_order_release);
592
649
  setFrameStage(FrameStage::Idle);
593
650
  const int64_t frameDoneUs = esp_timer_get_time();
651
+ #if GEA_EMBEDDED_COMPARISON_BENCHMARK
652
+ if (comparisonToken.active) {
653
+ std::uint32_t calls = 0;
654
+ std::uint64_t pixels = 0;
655
+ gea::platform::display::Display::flushOdometerRead(calls, pixels);
656
+ const std::uint32_t chunks = gea_display_completed_chunks ? gea_display_completed_chunks() : 0;
657
+ gea::platform::comparison::frameDone(comparisonToken, frameStartUs, frameDoneUs, calls - comparisonCalls,
658
+ pixels - comparisonPixels, chunks - comparisonChunks,
659
+ comparisonCompletionWaitUs, comparisonCompletionSucceeded);
660
+ }
661
+ #endif
594
662
  #if defined(GEA_EMBEDDED_FRAME_BENCHMARK) && GEA_EMBEDDED_FRAME_BENCHMARK
595
663
  gea_frame_benchmark_sample(frameStartUs, frameDoneUs);
596
664
  #endif
@@ -798,7 +866,11 @@ public:
798
866
  // (coalesced) rather than spinning a back-to-back loop on a second clock.
799
867
  const bool vsyncDriven = vsyncDriven_.load(std::memory_order_acquire);
800
868
  const bool catchUpRequested =
801
- !vsyncDriven && (timerCatchUpDue || (frameDoneUs - frameStartUs) >= frameIntervalUs());
869
+ !vsyncDriven
870
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
871
+ && debuggerFrameRate() == 0
872
+ #endif
873
+ && (timerCatchUpDue || (frameDoneUs - frameStartUs) >= frameIntervalUs());
802
874
  catchUpRequest_.store(catchUpRequested, std::memory_order_release);
803
875
  bool forceIdleYield = false;
804
876
  #if GEA_EMBEDDED_FRAME_SCHEDULER_MAX_CATCHUP_FRAMES_BEFORE_YIELD > 0
@@ -844,6 +916,34 @@ public:
844
916
  return intervalUs > 0 ? 1000000.0 / static_cast<double>(intervalUs) : 0.0;
845
917
  }
846
918
 
919
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
920
+ void setDebuggerFrameRate(int fps) {
921
+ if (fps < 0 || fps > 120) return;
922
+ debuggerFps_.store(fps, std::memory_order_relaxed);
923
+ nextDebuggerFrameUs_.store(0, std::memory_order_release);
924
+ #if GEA_EMBEDDED_FRAME_SCHEDULER_USE_ESP_TIMER
925
+ restartTimer(frameIntervalUs());
926
+ #endif
927
+ }
928
+ int debuggerFrameRate() const { return debuggerFps_.load(std::memory_order_relaxed); }
929
+ bool debuggerFrameDue(uint32_t now) const {
930
+ const int fps = debuggerFrameRate();
931
+ const uint32_t next = nextDebuggerFrameUs_.load(std::memory_order_acquire);
932
+ // RTOS dispatch jitter must not turn 10 fps into 5 by missing every
933
+ // second exact timer deadline. Preserve phase across early/late wakes.
934
+ return fps == 0 || next == 0 || static_cast<int32_t>(now - next) >= -1000;
935
+ }
936
+ void scheduleDebuggerFrame(uint32_t now) {
937
+ const int fps = debuggerFrameRate();
938
+ if (!fps) { nextDebuggerFrameUs_.store(0, std::memory_order_release); return; }
939
+ const uint32_t period = static_cast<uint32_t>((1000000 + fps - 1) / fps);
940
+ const uint32_t previous = nextDebuggerFrameUs_.load(std::memory_order_acquire);
941
+ uint32_t next = previous == 0 ? now + period : previous + period;
942
+ if (static_cast<int32_t>(now - next) >= 0) next += ((now - next) / period + 1) * period;
943
+ nextDebuggerFrameUs_.store(next, std::memory_order_release);
944
+ }
945
+
946
+ #endif
847
947
  private:
848
948
  struct PerfWindow {
849
949
  int frameCount = 0;
@@ -1054,13 +1154,18 @@ private:
1054
1154
  const int64_t normalized = normalizeFrameIntervalUs(intervalUs);
1055
1155
  frameIntervalUs_.store(normalized, std::memory_order_relaxed);
1056
1156
  #if GEA_EMBEDDED_FRAME_SCHEDULER_USE_ESP_TIMER
1057
- restartTimer(normalized);
1157
+ restartTimer(frameIntervalUs());
1058
1158
  #endif
1059
1159
  }
1060
1160
 
1061
1161
  int64_t frameIntervalUs() const
1062
1162
  {
1063
- return frameIntervalUs_.load(std::memory_order_relaxed);
1163
+ const auto interval = frameIntervalUs_.load(std::memory_order_relaxed);
1164
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
1165
+ const int fps = debuggerFrameRate();
1166
+ if (fps > 0) return std::max<int64_t>(interval, (1000000 + fps - 1) / fps);
1167
+ #endif
1168
+ return interval;
1064
1169
  }
1065
1170
 
1066
1171
  static int64_t normalizeFrameIntervalUs(int64_t intervalUs)
@@ -1140,6 +1245,9 @@ private:
1140
1245
 
1141
1246
  void queueFrameEvent()
1142
1247
  {
1248
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
1249
+ if (!debuggerFrameDue(static_cast<uint32_t>(esp_timer_get_time()))) return;
1250
+ #endif
1143
1251
  timerTickCount_.fetch_add(1, std::memory_order_relaxed);
1144
1252
  if (!eventQueue_)
1145
1253
  return;
@@ -1630,6 +1738,10 @@ private:
1630
1738
  #endif
1631
1739
  QueueHandle_t eventQueue_ = nullptr;
1632
1740
  std::atomic<int64_t> frameIntervalUs_{FrameScheduler::kDefaultFrameIntervalUs};
1741
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
1742
+ std::atomic<int> debuggerFps_{GEA_NATIVE_DEBUGGER_FPS};
1743
+ std::atomic<uint32_t> nextDebuggerFrameUs_{0};
1744
+ #endif
1633
1745
  PerfWindow perf_{};
1634
1746
  };
1635
1747
 
@@ -1718,4 +1830,16 @@ double FrameScheduler::frameRate()
1718
1830
  return FrameSchedulerEngine::instance().frameRate();
1719
1831
  }
1720
1832
 
1721
- } // namespace gea::framework::services
1833
+ #if defined(GEA_NATIVE_DEBUGGER) && GEA_NATIVE_DEBUGGER
1834
+ void FrameScheduler::setDebuggerFrameRate(int fps) { FrameSchedulerEngine::instance().setDebuggerFrameRate(fps); }
1835
+ int FrameScheduler::debuggerFrameRate() { return FrameSchedulerEngine::instance().debuggerFrameRate(); }
1836
+ #endif
1837
+
1838
+ #if GEA_EMBEDDED_COMPARISON_BENCHMARK
1839
+ extern "C" bool gea_frame_capture_boundary_ready()
1840
+ {
1841
+ return FrameSchedulerEngine::instance().diagnosticCaptureBoundaryReady();
1842
+ }
1843
+ #endif
1844
+
1845
+ } // namespace gea::framework::services
@@ -10,6 +10,8 @@
10
10
 
11
11
  #include "esp_heap_caps.h"
12
12
  #include "esp_rom_sys.h"
13
+ #include "freertos/FreeRTOS.h"
14
+ #include <cstdio>
13
15
 
14
16
  #include <cstddef>
15
17
  #include <cstdint>
@@ -18,6 +20,85 @@
18
20
 
19
21
  namespace {
20
22
 
23
+ #if defined(GEA_HEAP_TRACE)
24
+ // Diagnosis only: live bytes of C++ heap traffic by allocation size (log2
25
+ // buckets), printed by the runtime's cycle trace (gea_heap_trace_print), so
26
+ // a board whose heap drains names the size class that grows.
27
+ std::uint32_t g_traceBucket[24];
28
+ std::uint32_t g_traceLive = 0;
29
+
30
+ // Live bytes per call site (allocations >= 512 B): ptr -> (site, size) in an
31
+ // open-addressed table, site -> live bytes in a second one.
32
+ struct SiteSlot { std::uintptr_t key; std::uintptr_t site; std::uint32_t size; };
33
+ constexpr unsigned kPtrSlots = 16384, kSiteSlots = 1024;
34
+ SiteSlot *g_ptrTable = nullptr;
35
+ std::uintptr_t g_siteKey[kSiteSlots];
36
+ std::int32_t g_siteLive[kSiteSlots];
37
+ portMUX_TYPE g_traceLock = portMUX_INITIALIZER_UNLOCKED;
38
+ thread_local std::uintptr_t t_site = 0;
39
+
40
+ void traceSite(void *ptr, std::uint32_t size, bool add)
41
+ {
42
+ if (!g_ptrTable) {
43
+ if (!add) return;
44
+ g_ptrTable = static_cast<SiteSlot *>(heap_caps_calloc(kPtrSlots, sizeof(SiteSlot), MALLOC_CAP_SPIRAM));
45
+ if (!g_ptrTable) return;
46
+ }
47
+ const std::uintptr_t key = reinterpret_cast<std::uintptr_t>(ptr);
48
+ portENTER_CRITICAL(&g_traceLock);
49
+ unsigned h = static_cast<unsigned>((key >> 3) * 2654435761u) & (kPtrSlots - 1);
50
+ if (add) {
51
+ if (size >= 512 && t_site) {
52
+ for (unsigned i = 0; i < kPtrSlots; i++, h = (h + 1) & (kPtrSlots - 1)) {
53
+ if (g_ptrTable[h].key == 0 || g_ptrTable[h].key == 1) {
54
+ g_ptrTable[h] = {key, t_site, size};
55
+ unsigned s = static_cast<unsigned>((t_site >> 1) * 2654435761u) & (kSiteSlots - 1);
56
+ for (unsigned j = 0; j < kSiteSlots; j++, s = (s + 1) & (kSiteSlots - 1)) {
57
+ if (g_siteKey[s] == t_site || g_siteKey[s] == 0) { g_siteKey[s] = t_site; g_siteLive[s] += size; break; }
58
+ }
59
+ break;
60
+ }
61
+ }
62
+ }
63
+ } else {
64
+ for (unsigned i = 0; i < kPtrSlots; i++, h = (h + 1) & (kPtrSlots - 1)) {
65
+ if (g_ptrTable[h].key == 0) break;
66
+ if (g_ptrTable[h].key == key) {
67
+ const std::uintptr_t site = g_ptrTable[h].site;
68
+ unsigned s = static_cast<unsigned>((site >> 1) * 2654435761u) & (kSiteSlots - 1);
69
+ for (unsigned j = 0; j < kSiteSlots; j++, s = (s + 1) & (kSiteSlots - 1)) {
70
+ if (g_siteKey[s] == site) { g_siteLive[s] -= g_ptrTable[h].size; break; }
71
+ if (g_siteKey[s] == 0) break;
72
+ }
73
+ g_ptrTable[h].key = 1; // tombstone
74
+ break;
75
+ }
76
+ }
77
+ }
78
+ portEXIT_CRITICAL(&g_traceLock);
79
+ }
80
+
81
+ void traceHeap(void *ptr, bool add)
82
+ {
83
+ if (!ptr) return;
84
+ const std::uint32_t size = static_cast<std::uint32_t>(heap_caps_get_allocated_size(ptr));
85
+ traceSite(ptr, size, add);
86
+ t_site = 0;
87
+ unsigned bucket = 0;
88
+ while (bucket < 23 && (std::uint32_t{16} << bucket) < size) bucket++;
89
+ if (add) {
90
+ g_traceBucket[bucket] += size;
91
+ g_traceLive += size;
92
+ } else {
93
+ g_traceBucket[bucket] -= g_traceBucket[bucket] < size ? g_traceBucket[bucket] : size;
94
+ g_traceLive -= g_traceLive < size ? g_traceLive : size;
95
+ return;
96
+ }
97
+ }
98
+ #else
99
+ inline void traceHeap(void *, bool) {}
100
+ #endif
101
+
21
102
  class EspHeapAllocator {
22
103
  public:
23
104
  static void *allocatePreferSpiram(std::size_t size, std::size_t alignment = alignof(std::max_align_t))
@@ -27,6 +108,7 @@ public:
27
108
  void *ptr = allocateWithCaps(size, alignment, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
28
109
  if (!ptr) ptr = allocateWithCaps(size, alignment, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
29
110
  if (!ptr) ptr = allocateWithCaps(size, alignment, MALLOC_CAP_8BIT);
111
+ traceHeap(ptr, true);
30
112
  return ptr;
31
113
  }
32
114
 
@@ -65,7 +147,39 @@ private:
65
147
 
66
148
  } // namespace
67
149
 
150
+ #if defined(GEA_HEAP_TRACE)
151
+ extern "C" void gea_heap_trace_print()
152
+ {
153
+ // Top call sites by live bytes (and their change since the last print).
154
+ static std::int32_t lastLive[kSiteSlots];
155
+ unsigned top[12] = {};
156
+ int n = 0;
157
+ for (unsigned i = 0; i < kSiteSlots; i++) {
158
+ if (!g_siteKey[i] || g_siteLive[i] < 8 * 1024) continue;
159
+ int at = n < 12 ? n++ : 11;
160
+ if (at == 11 && n == 12 && g_siteLive[top[11]] >= g_siteLive[i]) continue;
161
+ top[at] = i;
162
+ while (at > 0 && g_siteLive[top[at - 1]] < g_siteLive[top[at]]) { unsigned t = top[at]; top[at] = top[at - 1]; top[at - 1] = t; at--; }
163
+ }
164
+ std::printf("[heaps]");
165
+ for (int k = 0; k < n; k++) {
166
+ const unsigned i = top[k];
167
+ std::printf(" %08x:%dK(%+d)", static_cast<unsigned>(g_siteKey[i]), static_cast<int>(g_siteLive[i] >> 10), static_cast<int>((g_siteLive[i] - lastLive[i]) >> 10));
168
+ }
169
+ for (unsigned i = 0; i < kSiteSlots; i++) lastLive[i] = g_siteLive[i];
170
+ std::printf("\n");
171
+ std::printf("[heapt] live=%uKB psram_free=%uKB", static_cast<unsigned>(g_traceLive >> 10),
172
+ static_cast<unsigned>(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) >> 10));
173
+ for (unsigned i = 0; i < 24; i++)
174
+ if (g_traceBucket[i] >= 16 * 1024) std::printf(" %u:%uK", static_cast<unsigned>(16u << i), static_cast<unsigned>(g_traceBucket[i] >> 10));
175
+ std::printf("\n");
176
+ }
177
+ #endif
178
+
68
179
  void *gea::framework::memory::Allocator::allocatePreferSpiram(std::size_t size, std::size_t alignment) {
180
+ #if defined(GEA_HEAP_TRACE)
181
+ t_site = reinterpret_cast<std::uintptr_t>(__builtin_return_address(0));
182
+ #endif
69
183
  return EspHeapAllocator::allocatePreferSpiram(size, alignment);
70
184
  }
71
185
 
@@ -75,18 +189,29 @@ void *gea::framework::memory::Allocator::reallocatePreferSpiram(void *ptr, std::
75
189
  return nullptr;
76
190
  }
77
191
 
192
+ traceHeap(ptr, false);
193
+ #if defined(GEA_HEAP_TRACE)
194
+ t_site = reinterpret_cast<std::uintptr_t>(__builtin_return_address(0));
195
+ #endif
78
196
  void *next = heap_caps_realloc(ptr, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
79
197
  if (!next) next = heap_caps_realloc(ptr, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
80
198
  if (!next) next = heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT);
199
+ traceHeap(next ? next : ptr, true);
81
200
  return next;
82
201
  }
83
202
 
84
203
  void gea::framework::memory::Allocator::free(void *ptr) noexcept {
204
+ traceHeap(ptr, false);
85
205
  if (ptr) heap_caps_free(ptr);
86
206
  }
87
207
 
208
+ #if defined(GEA_HEAP_TRACE)
209
+ void *operator new(std::size_t size) { t_site = reinterpret_cast<std::uintptr_t>(__builtin_return_address(0)); return EspHeapAllocator::allocateOrAbort(size); }
210
+ void *operator new[](std::size_t size) { t_site = reinterpret_cast<std::uintptr_t>(__builtin_return_address(0)); return EspHeapAllocator::allocateOrAbort(size); }
211
+ #else
88
212
  void *operator new(std::size_t size) { return EspHeapAllocator::allocateOrAbort(size); }
89
213
  void *operator new[](std::size_t size) { return EspHeapAllocator::allocateOrAbort(size); }
214
+ #endif
90
215
 
91
216
  void *operator new(std::size_t size, const std::nothrow_t &) noexcept {
92
217
  return EspHeapAllocator::allocatePreferSpiram(size);
@@ -2,7 +2,7 @@
2
2
  #pragma once
3
3
  #include <cstdio>
4
4
 
5
- #if GEA_EMBEDDED_APP_USES_AUDIO && __has_include("esp_opus_enc.h")
5
+ #if GEA_EMBEDDED_APP_USES_AUDIO && !defined(GEA_EMBEDDED_RTC_UNUSED) && __has_include("esp_opus_enc.h")
6
6
  #include "host/rtc_opus.h"
7
7
  #include "esp_timer.h"
8
8
  #include "esp_heap_caps.h"
@@ -1,4 +1,5 @@
1
1
  #include "services/storage_service.h"
2
+ #include "platform/file_cache.h"
2
3
 
3
4
  #include "esp_err.h"
4
5
  #include "esp_log.h"
@@ -34,126 +35,140 @@ constexpr const char *kSpiffsBase = "/storage";
34
35
  constexpr const char *kSpiffsPartition = "storage";
35
36
  constexpr const char *kKvPath = "/storage/ls_kv.blob";
36
37
  bool g_spiffs_ok = false;
38
+
39
+ bool flashStorageMounted() { return g_spiffs_ok; }
37
40
  #endif
38
41
 
39
- } // namespace
42
+ } // namespace
40
43
 
41
- bool StorageService::init()
42
- {
43
- esp_err_t ret = nvs_flash_init();
44
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
45
- ESP_ERROR_CHECK(nvs_flash_erase());
46
- ret = nvs_flash_init();
47
- }
48
- if (ret != ESP_OK) {
49
- ESP_LOGE(kTag, "NVS init failed: %s", esp_err_to_name(ret));
50
- return false;
51
- }
44
+ bool StorageService::init() {
45
+ esp_err_t ret = nvs_flash_init();
46
+ if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
47
+ ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
48
+ ESP_ERROR_CHECK(nvs_flash_erase());
49
+ ret = nvs_flash_init();
50
+ }
51
+ if (ret != ESP_OK) {
52
+ ESP_LOGE(kTag, "NVS init failed: %s", esp_err_to_name(ret));
53
+ return false;
54
+ }
52
55
  #if defined(GEA_STORAGE_SPIFFS)
53
- esp_vfs_spiffs_conf_t conf = {};
54
- conf.base_path = kSpiffsBase;
55
- conf.partition_label = kSpiffsPartition;
56
- conf.max_files = 4;
57
- conf.format_if_mount_failed = true;
58
- const esp_err_t sret = esp_vfs_spiffs_register(&conf);
59
- g_spiffs_ok = (sret == ESP_OK);
60
- if (g_spiffs_ok) {
61
- std::size_t total = 0, used = 0;
62
- if (esp_spiffs_info(kSpiffsPartition, &total, &used) == ESP_OK)
63
- ESP_LOGI(kTag, "SPIFFS mounted: %u/%u bytes used", static_cast<unsigned>(used),
64
- static_cast<unsigned>(total));
65
- } else {
66
- ESP_LOGW(kTag, "SPIFFS mount failed: %s (localStorage falls back to NVS)",
67
- esp_err_to_name(sret));
68
- }
56
+ esp_vfs_spiffs_conf_t conf = {};
57
+ conf.base_path = kSpiffsBase;
58
+ conf.partition_label = kSpiffsPartition;
59
+ conf.max_files = 4;
60
+ conf.format_if_mount_failed = true;
61
+ const esp_err_t sret = esp_vfs_spiffs_register(&conf);
62
+ g_spiffs_ok = (sret == ESP_OK);
63
+ gea::platform::storage::setFallbackMountProvider(&flashStorageMounted);
64
+ if (g_spiffs_ok) {
65
+ std::size_t total = 0, used = 0;
66
+ if (esp_spiffs_info(kSpiffsPartition, &total, &used) == ESP_OK)
67
+ ESP_LOGI(kTag, "SPIFFS mounted: %u/%u bytes used",
68
+ static_cast<unsigned>(used), static_cast<unsigned>(total));
69
+ } else {
70
+ ESP_LOGW(kTag, "SPIFFS mount failed: %s (localStorage falls back to NVS)",
71
+ esp_err_to_name(sret));
72
+ }
69
73
  #endif
70
- return true;
74
+ return true;
71
75
  }
72
76
 
73
- bool StorageService::getString(const char *key, char *buf, unsigned capacity)
74
- {
75
- if (!key || !buf || capacity == 0) return false;
76
- nvs_handle_t handle;
77
- if (nvs_open(kNamespace, NVS_READONLY, &handle) != ESP_OK) return false;
78
- std::size_t len = capacity;
79
- const esp_err_t err = nvs_get_str(handle, key, buf, &len);
80
- nvs_close(handle);
81
- return err == ESP_OK;
77
+ bool StorageService::getString(const char *key, char *buf, unsigned capacity) {
78
+ if (!key || !buf || capacity == 0)
79
+ return false;
80
+ nvs_handle_t handle;
81
+ if (nvs_open(kNamespace, NVS_READONLY, &handle) != ESP_OK)
82
+ return false;
83
+ std::size_t len = capacity;
84
+ const esp_err_t err = nvs_get_str(handle, key, buf, &len);
85
+ nvs_close(handle);
86
+ return err == ESP_OK;
82
87
  }
83
88
 
84
- bool StorageService::setString(const char *key, const char *value)
85
- {
86
- if (!key || !value) return false;
87
- nvs_handle_t handle;
88
- if (nvs_open(kNamespace, NVS_READWRITE, &handle) != ESP_OK) return false;
89
- esp_err_t err = nvs_set_str(handle, key, value);
90
- if (err == ESP_OK) err = nvs_commit(handle);
91
- nvs_close(handle);
92
- if (err != ESP_OK) ESP_LOGW(kTag, "setString(%s) failed: %s", key, esp_err_to_name(err));
93
- return err == ESP_OK;
89
+ bool StorageService::setString(const char *key, const char *value) {
90
+ if (!key || !value)
91
+ return false;
92
+ nvs_handle_t handle;
93
+ if (nvs_open(kNamespace, NVS_READWRITE, &handle) != ESP_OK)
94
+ return false;
95
+ esp_err_t err = nvs_set_str(handle, key, value);
96
+ if (err == ESP_OK)
97
+ err = nvs_commit(handle);
98
+ nvs_close(handle);
99
+ if (err != ESP_OK)
100
+ ESP_LOGW(kTag, "setString(%s) failed: %s", key, esp_err_to_name(err));
101
+ return err == ESP_OK;
94
102
  }
95
103
 
96
- bool StorageService::loadKv(std::string &out)
97
- {
98
- out.clear();
104
+ bool StorageService::loadKv(std::string &out) {
105
+ out.clear();
99
106
  #if defined(GEA_STORAGE_SPIFFS)
100
- if (g_spiffs_ok) {
101
- FILE *f = std::fopen(kKvPath, "rb");
102
- if (!f) return true; // no file yet == empty store (not an error)
103
- std::fseek(f, 0, SEEK_END);
104
- const long n = std::ftell(f);
105
- std::fseek(f, 0, SEEK_SET);
106
- if (n > 0) {
107
- out.resize(static_cast<std::size_t>(n));
108
- const std::size_t rd = std::fread(out.data(), 1, static_cast<std::size_t>(n), f);
109
- if (rd != static_cast<std::size_t>(n)) out.resize(rd);
110
- }
111
- std::fclose(f);
112
- return true;
113
- }
107
+ if (g_spiffs_ok) {
108
+ FILE *f = std::fopen(kKvPath, "rb");
109
+ if (!f)
110
+ return true; // no file yet == empty store (not an error)
111
+ std::fseek(f, 0, SEEK_END);
112
+ const long n = std::ftell(f);
113
+ std::fseek(f, 0, SEEK_SET);
114
+ if (n > 0) {
115
+ out.resize(static_cast<std::size_t>(n));
116
+ const std::size_t rd =
117
+ std::fread(out.data(), 1, static_cast<std::size_t>(n), f);
118
+ if (rd != static_cast<std::size_t>(n))
119
+ out.resize(rd);
120
+ }
121
+ std::fclose(f);
122
+ return true;
123
+ }
114
124
  #endif
115
- nvs_handle_t handle;
116
- if (nvs_open(kNamespace, NVS_READONLY, &handle) != ESP_OK) return false;
117
- std::size_t len = 0;
118
- esp_err_t err = nvs_get_blob(handle, kLocalStorageKey, nullptr, &len);
119
- if (err == ESP_OK && len > 0) {
120
- out.resize(len);
121
- err = nvs_get_blob(handle, kLocalStorageKey, out.data(), &len);
122
- if (err != ESP_OK) out.clear();
123
- }
124
- nvs_close(handle);
125
- return err == ESP_OK;
125
+ nvs_handle_t handle;
126
+ if (nvs_open(kNamespace, NVS_READONLY, &handle) != ESP_OK)
127
+ return false;
128
+ std::size_t len = 0;
129
+ esp_err_t err = nvs_get_blob(handle, kLocalStorageKey, nullptr, &len);
130
+ if (err == ESP_OK && len > 0) {
131
+ out.resize(len);
132
+ err = nvs_get_blob(handle, kLocalStorageKey, out.data(), &len);
133
+ if (err != ESP_OK)
134
+ out.clear();
135
+ }
136
+ nvs_close(handle);
137
+ return err == ESP_OK;
126
138
  }
127
139
 
128
- void StorageService::saveKv(const std::string &blob)
129
- {
140
+ void StorageService::saveKv(const std::string &blob) {
130
141
  #if defined(GEA_STORAGE_SPIFFS)
131
- if (g_spiffs_ok) {
132
- if (blob.empty()) {
133
- std::remove(kKvPath);
134
- return;
135
- }
136
- FILE *f = std::fopen(kKvPath, "wb");
137
- if (!f) {
138
- ESP_LOGW(kTag, "saveKv: fopen(%s) failed", kKvPath);
139
- return;
140
- }
141
- const std::size_t wr = std::fwrite(blob.data(), 1, blob.size(), f);
142
- std::fclose(f);
143
- if (wr != blob.size()) ESP_LOGW(kTag, "saveKv: short write %u/%u", static_cast<unsigned>(wr),
144
- static_cast<unsigned>(blob.size()));
145
- return;
146
- }
142
+ if (g_spiffs_ok) {
143
+ if (blob.empty()) {
144
+ std::remove(kKvPath);
145
+ return;
146
+ }
147
+ FILE *f = std::fopen(kKvPath, "wb");
148
+ if (!f) {
149
+ ESP_LOGW(kTag, "saveKv: fopen(%s) failed", kKvPath);
150
+ return;
151
+ }
152
+ const std::size_t wr = std::fwrite(blob.data(), 1, blob.size(), f);
153
+ std::fclose(f);
154
+ if (wr != blob.size())
155
+ ESP_LOGW(kTag, "saveKv: short write %u/%u", static_cast<unsigned>(wr),
156
+ static_cast<unsigned>(blob.size()));
157
+ return;
158
+ }
147
159
  #endif
148
- nvs_handle_t handle;
149
- if (nvs_open(kNamespace, NVS_READWRITE, &handle) != ESP_OK) return;
150
- esp_err_t err = blob.empty() ? nvs_erase_key(handle, kLocalStorageKey)
151
- : nvs_set_blob(handle, kLocalStorageKey, blob.data(), blob.size());
152
- // Erasing a never-written key is fine — still commit so the store is durable.
153
- if (err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND) err = nvs_commit(handle);
154
- nvs_close(handle);
155
- if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND)
156
- ESP_LOGW(kTag, "saveKv failed: %s", esp_err_to_name(err));
160
+ nvs_handle_t handle;
161
+ if (nvs_open(kNamespace, NVS_READWRITE, &handle) != ESP_OK)
162
+ return;
163
+ esp_err_t err = blob.empty() ? nvs_erase_key(handle, kLocalStorageKey)
164
+ : nvs_set_blob(handle, kLocalStorageKey,
165
+ blob.data(), blob.size());
166
+ // Erasing a never-written key is fine — still commit so the store is durable.
167
+ if (err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND)
168
+ err = nvs_commit(handle);
169
+ nvs_close(handle);
170
+ if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND)
171
+ ESP_LOGW(kTag, "saveKv failed: %s", esp_err_to_name(err));
157
172
  }
158
173
 
159
- } // namespace gea::framework::services
174
+ } // namespace gea::framework::services