@geastack/targets 0.1.110 → 0.1.111

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/targets",
3
- "version": "0.1.110",
3
+ "version": "0.1.111",
4
4
  "description": "Native embedded build targets (ESP32-IDF, RP2350) for geastack apps: board sources, target catalog, and CMake glue driven by the gea CLI.",
5
5
  "license": "GPL-3.0-only",
6
6
  "repository": {
@@ -36,11 +36,11 @@
36
36
  "access": "public"
37
37
  },
38
38
  "dependencies": {
39
- "@geastack/chips": "^0.1.7",
39
+ "@geastack/chips": "^0.1.8",
40
40
  "@geastack/compiler": "^1.0.41",
41
41
  "@geastack/core": "0.1.53",
42
42
  "@geastack/elements": "^0.1.6",
43
- "@geastack/engine": "^0.1.23",
43
+ "@geastack/engine": "^0.1.24",
44
44
  "@geastack/geaos": "^0.1.3",
45
45
  "@geastack/geatsc-plugin-gea": "0.1.28",
46
46
  "@geastack/host": "0.1.23"
@@ -53,8 +53,8 @@ namespace {
53
53
  constexpr char kTag[] = "ns4168";
54
54
  constexpr auto &kAudio = gea::platform::board::audio;
55
55
  constexpr bool kHasMicrophone = kAudio.micBclk != GPIO_NUM_NC && kAudio.micDin != GPIO_NUM_NC;
56
- constexpr i2s_port_t kSpeakerPort = I2S_NUM_1;
57
- constexpr i2s_port_t kMicPort = I2S_NUM_0;
56
+ constexpr int kSpeakerPort = I2S_NUM_1; // i2s_chan_config_t::id is a plain int (IDF 6 dropped i2s_port_t)
57
+ constexpr int kMicPort = I2S_NUM_0;
58
58
  // The INMP441's 24 significant bits sit at the top of the 32-bit slot; keeping
59
59
  // bits 29..14 gives ~12 dB of headroom over the raw top 16 bits, which is where
60
60
  // speech from arm's length lands for this part.
@@ -87,6 +87,17 @@
87
87
  #include "soc/gdma_struct.h"
88
88
  #include "esp_async_memcpy.h"
89
89
  #endif
90
+ #if CONFIG_IDF_TARGET_ESP32P4 && GEA_EMBEDDED_RGB_SYNC_MONITOR
91
+ #include "soc/axi_dma_struct.h"
92
+ #endif
93
+ #if CONFIG_IDF_TARGET_ESP32P4 && !GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
94
+ // Full-width scroll copies between the scanned buffers go through the AXI-GDMA
95
+ // (see dmaScrollCopy).
96
+ #define GEA_RGB_DMA_SCROLL 1
97
+ #include "esp_async_memcpy.h"
98
+ #else
99
+ #define GEA_RGB_DMA_SCROLL 0
100
+ #endif
90
101
 
91
102
  #include <algorithm>
92
103
  #include <atomic>
@@ -499,6 +510,9 @@ void applyCanvasMap()
499
510
  // flight the panel has read (see backRowReleased).
500
511
  volatile std::int64_t g_vsyncUs = 0;
501
512
  volatile std::int64_t g_frameCompleteUs = 0;
513
+ // The driver fills its own bounce buffers (a board names bounceRows on a
514
+ // direct-scanout target): it restarts a short frame itself.
515
+ bool g_driverBounce = false;
502
516
 
503
517
  // VSYNC interrupt lateness: the interval between VSYNC interrupts against
504
518
  // the frame period (a late one is a late DMA restart).
@@ -529,6 +543,21 @@ DRAM_ATTR std::uint32_t g_vsyncPhaseQ4 = 0;
529
543
  // A VSYNC interrupt this late (past the hardware event, in 1/16 us) may
530
544
  // already be inside the next frame's first lines: no restart then.
531
545
  constexpr std::uint32_t kVsyncOnTimeQ4 = 40 * 16;
546
+ DRAM_ATTR std::uint32_t g_slipLogMs[16] = {}, g_slipLogN = 0;
547
+ DRAM_ATTR int g_slipSinceSwap[16] = {};
548
+ volatile std::int64_t g_lastSwapUs = 0;
549
+ volatile std::uint32_t g_swapCount = 0;
550
+ DRAM_ATTR std::int32_t g_slipLogUs[16] = {};
551
+ DRAM_ATTR std::int32_t g_eofGapMin = 1 << 30, g_eofGapMax = -(1 << 30), g_eofGapLast = 0;
552
+ // Where the panel DMA is fetching at VSYNC, in bytes from the start of the
553
+ // frame (negative = still in the previous frame): a lag that grows is a drift.
554
+ DRAM_ATTR std::int32_t g_dmaPosMin = 1 << 30, g_dmaPosMax = -(1 << 30), g_dmaPosLast = 0;
555
+ DRAM_ATTR std::uint32_t g_dmaPosUnknown = 0;
556
+ // Tasks running on each core at the VSYNC before, and the one that found, a lag.
557
+ DRAM_ATTR const char *g_lagTask[4] = {};
558
+ DRAM_ATTR const char *g_prevTask[2] = {};
559
+ DRAM_ATTR std::uint32_t g_lagMs = 0;
560
+ DRAM_ATTR std::uint32_t g_vsyncLate40 = 0, g_vsyncLate200us = 0, g_vsyncLate700 = 0, g_vsyncLateMaxQ4 = 0;
532
561
  int panelDmaBin();
533
562
  IRAM_ATTR bool onPanelVsync(esp_lcd_panel_handle_t panel, const esp_lcd_rgb_panel_event_data_t *, void *)
534
563
  {
@@ -570,6 +599,53 @@ IRAM_ATTR bool onPanelVsync(esp_lcd_panel_handle_t panel, const esp_lcd_rgb_pane
570
599
  if (lateQ4 <= kVsyncOnTimeQ4) g_vsyncPhaseQ4 = expected + lateQ4 / 4; // follow drift slowly
571
600
  }
572
601
  }
602
+ #if GEA_EMBEDDED_RGB_SYNC_MONITOR
603
+ // Scan diagnostics: how long before this VSYNC the DMA finished the
604
+ // frame (in step it is a fixed ~0.7 ms on the CrowPanel 5.0; a late or
605
+ // missing frame end is a starved DMA), and interrupt lateness.
606
+ {
607
+ const std::int32_t gap = static_cast<std::int32_t>(now - g_frameCompleteUs);
608
+ if (gap < g_eofGapMin) g_eofGapMin = gap;
609
+ if (gap > g_eofGapMax) g_eofGapMax = gap;
610
+ if (g_eofGapLast != 0 && (gap < 600 || gap > 900) && g_slipLogN < 16) {
611
+ g_slipLogMs[g_slipLogN] = static_cast<std::uint32_t>(now / 1000);
612
+ g_slipLogUs[g_slipLogN] = gap;
613
+ g_slipSinceSwap[g_slipLogN] = static_cast<int>((now - g_lastSwapUs) / 1000);
614
+ g_slipLogN++;
615
+ }
616
+ g_eofGapLast = gap;
617
+ #if CONFIG_IDF_TARGET_ESP32P4
618
+ {
619
+ std::int32_t pos = 0;
620
+ bool found = false;
621
+ for (int ch = 0; ch < 3 && !found; ++ch) {
622
+ const std::uint32_t desc = AXI_DMA.out[ch].conf.out_dscr.val;
623
+ if (desc < 0x4FF00000u || desc >= 0x4FFC0000u) continue;
624
+ const std::uintptr_t b = reinterpret_cast<const volatile std::uint32_t *>(desc)[1];
625
+ for (int i = 0; i < g_scanFbCount && !found; ++i) {
626
+ const auto lo = reinterpret_cast<std::uintptr_t>(g_scanFb[i]);
627
+ if (lo && b >= lo && b < lo + kFramebufferBytes) {
628
+ pos = static_cast<std::int32_t>(b - lo);
629
+ if (pos > static_cast<std::int32_t>(kFramebufferBytes / 2)) pos -= static_cast<std::int32_t>(kFramebufferBytes);
630
+ found = true;
631
+ }
632
+ }
633
+ }
634
+ if (found) {
635
+ if (pos < g_dmaPosMin) g_dmaPosMin = pos;
636
+ if (pos > g_dmaPosMax) g_dmaPosMax = pos;
637
+ g_dmaPosLast = pos;
638
+ } else {
639
+ g_dmaPosUnknown++;
640
+ }
641
+ }
642
+ #endif
643
+ }
644
+ if (lateQ4 > 40 * 16) g_vsyncLate40++;
645
+ if (lateQ4 > 200 * 16) g_vsyncLate200us++;
646
+ if (lateQ4 > 700 * 16) g_vsyncLate700++;
647
+ if (lateQ4 > g_vsyncLateMaxQ4) g_vsyncLateMaxQ4 = lateQ4;
648
+ #endif
573
649
  const int bin = panelDmaBin();
574
650
  if (bin >= 0 && bin < 32) g_vsyncAt[bin]++;
575
651
  // An interrupt in time that finds the DMA anywhere but buffer 0 /
@@ -593,6 +669,7 @@ IRAM_ATTR bool onPanelVsync(esp_lcd_panel_handle_t panel, const esp_lcd_rgb_pane
593
669
  g_prevLateUs = lateUs;
594
670
  g_prevEofFills = static_cast<std::uint8_t>(std::min<std::uint32_t>(fills, 255));
595
671
  }
672
+ #if GEA_EMBEDDED_DISPLAY_RGB_BOUNCE
596
673
  if (lateQ4 <= kVsyncOnTimeQ4) {
597
674
  if (bin != 1) g_vsyncRealigned++;
598
675
  if (esp_lcd_rgb_panel_restart(panel) == ESP_OK) {
@@ -601,6 +678,32 @@ IRAM_ATTR bool onPanelVsync(esp_lcd_panel_handle_t panel, const esp_lcd_rgb_pane
601
678
  } else {
602
679
  g_vsyncSkipped++;
603
680
  }
681
+ #else
682
+ // Direct scanout: in step, the DMA finishes each frame during the
683
+ // front porch, ~0.7 ms before this VSYNC. A FIFO underrun leaves it a
684
+ // few bytes behind for good -- it then finishes ~0.9 ms AFTER VSYNC,
685
+ // every frame, and the picture sits shifted right; each later underrun
686
+ // adds to it (a slow slide). So when no frame end came in the last
687
+ // 3 ms, restart it now: the driver does so right after this callback,
688
+ // at this same VSYNC, into the buffer currently shown. In step it is
689
+ // never touched.
690
+ const bool lagging = !g_driverBounce && g_frameCompleteUs != 0 && now - g_frameCompleteUs > 3000;
691
+ #if GEA_EMBEDDED_RGB_SYNC_MONITOR
692
+ {
693
+ const char *t0 = pcTaskGetName(xTaskGetCurrentTaskHandleForCore(0));
694
+ const char *t1 = pcTaskGetName(xTaskGetCurrentTaskHandleForCore(1));
695
+ if (lagging && g_lagMs == 0) {
696
+ g_lagTask[0] = g_prevTask[0]; g_lagTask[1] = g_prevTask[1];
697
+ g_lagTask[2] = t0; g_lagTask[3] = t1;
698
+ g_lagMs = static_cast<std::uint32_t>(now / 1000);
699
+ }
700
+ g_prevTask[0] = t0; g_prevTask[1] = t1;
701
+ }
702
+ #endif
703
+ if (lagging && esp_lcd_rgb_panel_restart(panel) == ESP_OK) {
704
+ g_vsyncRestarts++;
705
+ }
706
+ #endif
604
707
  }
605
708
  if (g_vsyncUs != 0) {
606
709
  const auto &d = gea::platform::board::display;
@@ -911,10 +1014,26 @@ void commitSwap(int back)
911
1014
  taskEXIT_CRITICAL(&g_swapMux);
912
1015
  #else
913
1016
  // Direct scanout: draw_bitmap writes the whole frame back from the cache
914
- // (too long for a critical section), and the GDMA may prefetch one more pass
915
- // of the old buffer, so always allow two frame ends.
1017
+ // (too long for a critical section), then relinks the tail of every frame's
1018
+ // descriptor chain to the new buffer. The GDMA reads a descriptor's link a
1019
+ // few descriptors (a few lines) ahead, so a relink well before the current
1020
+ // frame's last line takes effect at the next frame end, which then releases
1021
+ // the old front. A relink close to it may miss, and the old buffer is sent
1022
+ // once more: allow two frame ends then. Always waiting two held the next
1023
+ // back buffer up to a whole frame longer than needed, and a tab swipe on
1024
+ // the CrowPanel 5.0 dropped every third frame on that wait.
1025
+ const std::uint32_t seqBefore = g_frameSeq;
916
1026
  esp_lcd_panel_draw_bitmap(g_panel, 0, 0, kWidth, kHeight, g_scanFb[back]);
917
- seq = g_frameSeq + 2;
1027
+ const std::int64_t relinkedUs = esp_timer_get_time();
1028
+ const std::int64_t lastFrameEndUs = g_frameCompleteUs;
1029
+ g_lastSwapUs = relinkedUs;
1030
+ g_swapCount = g_swapCount + 1;
1031
+ const auto &d = gea::platform::board::display;
1032
+ constexpr std::int64_t kRelinkMarginUs = 1500;
1033
+ const std::int64_t frameUs = static_cast<std::int64_t>(kWidth + d.hsyncPulseWidth + d.hsyncBackPorch + d.hsyncFrontPorch) *
1034
+ (kHeight + d.vsyncPulseWidth + d.vsyncBackPorch + d.vsyncFrontPorch) * 1000000 / kPixelClockHz;
1035
+ const bool relinkedEarly = g_frameSeq == seqBefore && lastFrameEndUs != 0 && relinkedUs < lastFrameEndUs + frameUs - kRelinkMarginUs;
1036
+ seq = g_frameSeq + (relinkedEarly ? 1 : 2);
918
1037
  #endif
919
1038
  g_freeSeq[g_frontIndex] = seq;
920
1039
  std::uint16_t *oldBack = g_scanFb[back];
@@ -933,9 +1052,27 @@ void commitSwap(int back)
933
1052
  // One line per kSwapDiagFrames published frames, the same in both modes:
934
1053
  // publish = whole Display flush call, raster = fused raster inside it, copy =
935
1054
  // pixel copies into scanout memory, wait = blocked on the panel releasing the back buffer.
1055
+ #if GEA_RGB_FRAME_TRACE
1056
+ struct FrameTrace { std::uint32_t ms; std::int32_t dtUs, copyUs, waitUs, pubUs, px, patchPx, rasterUs; std::uint8_t path; };
1057
+ FrameTrace g_trace[128];
1058
+ volatile std::uint32_t g_traceHead = 0;
1059
+ std::int64_t g_tracePrevUs = 0, g_tracePrevCopy = 0, g_tracePrevWait = 0, g_tracePrevPatch = 0, g_tracePrevRaster = 0;
1060
+ #endif
936
1061
  void noteFrameDiag(int path, std::int64_t totalUs, int px)
937
1062
  {
938
1063
  if (px <= 0) return;
1064
+ #if GEA_RGB_FRAME_TRACE
1065
+ {
1066
+ const std::int64_t now = esp_timer_get_time();
1067
+ FrameTrace &t = g_trace[g_traceHead % 128];
1068
+ t = {static_cast<std::uint32_t>(now / 1000), static_cast<std::int32_t>(g_tracePrevUs ? now - g_tracePrevUs : 0),
1069
+ static_cast<std::int32_t>(g_swapDiag.copyUs - g_tracePrevCopy), static_cast<std::int32_t>(g_swapDiag.waitUs - g_tracePrevWait),
1070
+ static_cast<std::int32_t>(totalUs), px, static_cast<std::int32_t>(g_swapDiag.patchPx - g_tracePrevPatch), static_cast<std::int32_t>(g_swapDiag.rasterUs - g_tracePrevRaster), static_cast<std::uint8_t>(path)};
1071
+ g_traceHead = g_traceHead + 1;
1072
+ g_tracePrevUs = now;
1073
+ g_tracePrevCopy = g_swapDiag.copyUs; g_tracePrevWait = g_swapDiag.waitUs; g_tracePrevPatch = g_swapDiag.patchPx; g_tracePrevRaster = g_swapDiag.rasterUs;
1074
+ }
1075
+ #endif
939
1076
  g_swapDiag.paths[path]++;
940
1077
  g_swapDiag.totalUs += totalUs;
941
1078
  g_swapDiag.px += px;
@@ -966,6 +1103,9 @@ void noteFrameDiag(int path, std::int64_t totalUs, int px)
966
1103
  n * 1000000.0 / static_cast<double>(now - g_swapDiag.windowStartUs));
967
1104
  g_swapDiag = {};
968
1105
  g_swapDiag.windowStartUs = now;
1106
+ #if GEA_RGB_FRAME_TRACE
1107
+ g_tracePrevCopy = g_tracePrevWait = g_tracePrevPatch = g_tracePrevRaster = 0;
1108
+ #endif
969
1109
  g_diagVsync0 = vs;
970
1110
  g_diagFrame0 = fr;
971
1111
  }
@@ -1017,6 +1157,56 @@ void scanoutRowCopy(void *dst, const void *src, std::size_t bytes)
1017
1157
  }
1018
1158
 
1019
1159
  // Copy rect r of the front buffer into the back buffer.
1160
+ #if GEA_RGB_DMA_SCROLL
1161
+ async_memcpy_handle_t g_scrollDma = nullptr;
1162
+ bool g_scrollDmaFailed = false;
1163
+
1164
+ bool scrollDmaReady()
1165
+ {
1166
+ if (g_scrollDma) return true;
1167
+ if (g_scrollDmaFailed) return false;
1168
+ async_memcpy_config_t cfg = ASYNC_MEMCPY_DEFAULT_CONFIG();
1169
+ cfg.dma_burst_size = 128;
1170
+ if (esp_async_memcpy_install_gdma_axi(&cfg, &g_scrollDma) != ESP_OK) {
1171
+ g_scrollDmaFailed = true;
1172
+ g_scrollDma = nullptr;
1173
+ ESP_LOGW(kTag, "AXI-GDMA memcpy unavailable; scroll copies stay on the CPU");
1174
+ return false;
1175
+ }
1176
+ return true;
1177
+ }
1178
+
1179
+ bool IRAM_ATTR onScrollDmaDone(async_memcpy_handle_t, async_memcpy_event_t *, void *arg)
1180
+ {
1181
+ BaseType_t woken = pdFALSE;
1182
+ xSemaphoreGiveFromISR(static_cast<SemaphoreHandle_t>(arg), &woken);
1183
+ return woken == pdTRUE;
1184
+ }
1185
+
1186
+ // A scroll's shifted copy from the front buffer to the back, as one AXI-GDMA
1187
+ // transfer of contiguous memory. The CPU copy costs three PSRAM passes a pixel
1188
+ // (the write-allocate fill, the write-back and the read): ~12 ms for a tab
1189
+ // swipe's 800x410 viewport on the CrowPanel 5.0, most of a 61 Hz frame. The DMA
1190
+ // reads and writes each pixel once in 128-byte bursts, past the cache: 640 KB
1191
+ // in 4.6 ms. (The PPA's block move was no faster than the CPU.) IDF writes the
1192
+ // source back and invalidates the destination first.
1193
+ bool dmaScrollCopy(const std::uint16_t *src, std::uint16_t *dst, std::size_t bytes)
1194
+ {
1195
+ if (bytes == 0 || !scrollDmaReady()) return false;
1196
+ // Either core may copy (a patch can run on the row worker): each call waits
1197
+ // on its own semaphore.
1198
+ StaticSemaphore_t doneStorage;
1199
+ SemaphoreHandle_t done = xSemaphoreCreateBinaryStatic(&doneStorage);
1200
+ if (esp_async_memcpy(g_scrollDma, dst, const_cast<std::uint16_t *>(src), bytes, onScrollDmaDone, done) != ESP_OK) {
1201
+ vSemaphoreDelete(done);
1202
+ return false;
1203
+ }
1204
+ xSemaphoreTake(done, portMAX_DELAY);
1205
+ vSemaphoreDelete(done);
1206
+ return true;
1207
+ }
1208
+ #endif
1209
+
1020
1210
  void patchRect(const StaleRect &r)
1021
1211
  {
1022
1212
  const std::uint16_t *src = g_scanFb[g_frontIndex];
@@ -1024,6 +1214,17 @@ void patchRect(const StaleRect &r)
1024
1214
  const std::size_t bytes = static_cast<std::size_t>(r.x1 - r.x0 + 1) * sizeof(std::uint16_t);
1025
1215
  const ScanMap &ms = g_map[g_frontIndex];
1026
1216
  const ScanMap &md = g_map[g_drawIndex];
1217
+ #if GEA_RGB_DMA_SCROLL
1218
+ // Full-width rows laid out alike in both buffers are one contiguous block:
1219
+ // the DMA copies it (see dmaScrollCopy). A gesture's first frame patches the
1220
+ // whole stale buffer, 16 ms on the CPU and 5.5 ms this way.
1221
+ constexpr int kMinDmaRows = 16;
1222
+ if (r.x0 == 0 && r.x1 == kWidth - 1 && r.y1 - r.y0 + 1 >= kMinDmaRows && mapRow(ms, r.y0) == r.y0 && mapRow(md, r.y0) == r.y0 &&
1223
+ mapRow(ms, r.y1) == r.y1 && mapRow(md, r.y1) == r.y1 && (ms.h == 0 || ms.off == 0) && (md.h == 0 || md.off == 0)) {
1224
+ const std::size_t at = static_cast<std::size_t>(r.y0) * kWidth;
1225
+ if (dmaScrollCopy(&src[at], &dst[at], static_cast<std::size_t>(r.y1 - r.y0 + 1) * kWidth * sizeof(std::uint16_t))) return;
1226
+ }
1227
+ #endif
1027
1228
  for (int y = r.y0; y <= r.y1; ++y) {
1028
1229
  scanoutRowCopy(&dst[static_cast<std::size_t>(mapRow(md, y)) * kWidth + r.x0],
1029
1230
  &src[static_cast<std::size_t>(mapRow(ms, y)) * kWidth + r.x0], bytes);
@@ -1477,6 +1678,9 @@ esp_err_t initRgbPanel()
1477
1678
  config.timings.vsync_back_porch = display.vsyncBackPorch;
1478
1679
  config.timings.vsync_front_porch = display.vsyncFrontPorch;
1479
1680
  config.timings.flags.pclk_active_neg = display.pclkActiveNeg ? 1 : 0;
1681
+ #ifdef GEA_EMBEDDED_DISPLAY_RGB_PCLK_IDLE_HIGH
1682
+ config.timings.flags.pclk_idle_high = GEA_EMBEDDED_DISPLAY_RGB_PCLK_IDLE_HIGH;
1683
+ #endif
1480
1684
  config.data_width = 16;
1481
1685
  config.in_color_format = LCD_COLOR_FMT_RGB565;
1482
1686
  config.out_color_format = LCD_COLOR_FMT_RGB565;
@@ -1505,7 +1709,20 @@ esp_err_t initRgbPanel()
1505
1709
  }
1506
1710
  if (ownFbs < wantFbs) ESP_LOGW(kTag, "no room for %d scanout buffers; using %d", wantFbs, ownFbs);
1507
1711
  #else
1508
- config.bounce_buffer_size_px = 0;
1712
+ // Direct scanout (the P4): the driver owns the framebuffers and scans them
1713
+ // straight out of PSRAM with only a small FIFO, so a PSRAM stall of a few
1714
+ // microseconds underruns it and the picture shifts right (see onPanelVsync
1715
+ // for the recovery). On the CrowPanel 5.0 slow flash was such a stall:
1716
+ // at DIO 40MHz it underran every few seconds; at its QIO 80MHz, never.
1717
+ // Two swapping buffers, as in Elecrow's firmware: one buffer adds a
1718
+ // PSRAM-to-PSRAM copy of each changed region. Driver-filled bounce
1719
+ // buffers, when a board names rows, could not keep up with the render
1720
+ // load on the CrowPanel 5.0 (frames dropped en masse).
1721
+ {
1722
+ const int rows = display.bounceRows;
1723
+ config.bounce_buffer_size_px = rows > 0 && kHeight % rows == 0 ? static_cast<std::size_t>(kWidth) * rows : 0;
1724
+ g_driverBounce = config.bounce_buffer_size_px != 0;
1725
+ }
1509
1726
  #endif
1510
1727
  config.dma_burst_size = 64;
1511
1728
  config.hsync_gpio_num = display.hsync;
@@ -1633,6 +1850,67 @@ esp_err_t initRgbPanel()
1633
1850
  g_stripBuffer ? "ON" : "OFF (no internal SRAM)", g_stripBuffer, g_stripRows, g_stripHalves,
1634
1851
  static_cast<unsigned>(heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)),
1635
1852
  static_cast<unsigned>(heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)));
1853
+ #endif
1854
+ #if !GEA_EMBEDDED_DISPLAY_RGB_BOUNCE && GEA_EMBEDDED_RGB_SYNC_MONITOR
1855
+ // Scan health, every 2 s: in step, the driver completes exactly one frame
1856
+ // per VSYNC; a difference means the DMA fell out of step with the panel.
1857
+ xTaskCreatePinnedToCore([](void *) {
1858
+ std::uint32_t v0 = g_vsyncSeq, f0 = g_frameSeq;
1859
+ for (;;) {
1860
+ vTaskDelay(pdMS_TO_TICKS(2000));
1861
+ const std::uint32_t v = g_vsyncSeq, f = g_frameSeq;
1862
+ std::printf("[rgb-sync] vsync=%u frames=%u diff=%d late>40us=%u >200us=%u >700us=%u max=%uus eofgap=%d..%d last=%d swaps=%u dmapos=%d..%d last=%d unknown=%u restarts=%u\n", unsigned(v - v0), unsigned(f - f0), int((v - v0) - (f - f0)), unsigned(g_vsyncLate40), unsigned(g_vsyncLate200us), unsigned(g_vsyncLate700), unsigned(g_vsyncLateMaxQ4 >> 4), int(g_eofGapMin), int(g_eofGapMax), int(g_eofGapLast), unsigned(g_swapCount), int(g_dmaPosMin), int(g_dmaPosMax), int(g_dmaPosLast), unsigned(g_dmaPosUnknown), unsigned(g_vsyncRestarts));
1863
+ g_vsyncRestarts = 0;
1864
+ if (g_lagMs) {
1865
+ std::printf("[rgb-lag] t=%ums before: c0=%s c1=%s at: c0=%s c1=%s\n", unsigned(g_lagMs), g_lagTask[0] ? g_lagTask[0] : "?", g_lagTask[1] ? g_lagTask[1] : "?", g_lagTask[2] ? g_lagTask[2] : "?", g_lagTask[3] ? g_lagTask[3] : "?");
1866
+ g_lagMs = 0;
1867
+ }
1868
+ g_dmaPosMin = 1 << 30; g_dmaPosMax = -(1 << 30); g_dmaPosUnknown = 0;
1869
+ g_swapCount = 0;
1870
+ g_eofGapMin = 1 << 30; g_eofGapMax = -(1 << 30);
1871
+ for (std::uint32_t i = 0; i < g_slipLogN; ++i) std::printf("[rgb-slip] t=%ums gap=%dus since_swap=%dms\n", unsigned(g_slipLogMs[i]), int(g_slipLogUs[i]), g_slipSinceSwap[i]);
1872
+ g_slipLogN = 0;
1873
+ g_vsyncLate40 = g_vsyncLate200us = g_vsyncLate700 = g_vsyncLateMaxQ4 = 0;
1874
+ v0 = v;
1875
+ f0 = f;
1876
+ }
1877
+ }, "rgb_sync", 3072, nullptr, 1, nullptr, 0);
1878
+ #endif
1879
+ #if GEA_RGB_FRAME_TRACE
1880
+ {
1881
+ // Bandwidth probe: 640 KB copies between the two scan buffers / SRAM.
1882
+ const std::size_t n = 640 * 1024;
1883
+ auto *a = static_cast<std::uint8_t *>(heap_caps_aligned_alloc(64, n, MALLOC_CAP_SPIRAM));
1884
+ auto *b = static_cast<std::uint8_t *>(heap_caps_aligned_alloc(64, n, MALLOC_CAP_SPIRAM));
1885
+ auto *sram = static_cast<std::uint8_t *>(heap_caps_aligned_alloc(64, 32768, MALLOC_CAP_INTERNAL));
1886
+ if (a && b && sram) {
1887
+ std::memset(a, 1, n); std::memset(b, 2, n);
1888
+ std::int64_t t0 = esp_timer_get_time(); std::memcpy(b, a, n); std::int64_t t1 = esp_timer_get_time();
1889
+ for (std::size_t o = 0; o < n; o += 32768) std::memcpy(b + o, sram, 32768);
1890
+ std::int64_t t2 = esp_timer_get_time();
1891
+ for (std::size_t o = 0; o < n; o += 32768) std::memcpy(sram, a + o, 32768);
1892
+ std::int64_t t3 = esp_timer_get_time();
1893
+ std::memset(b, 3, n); std::int64_t t4 = esp_timer_get_time();
1894
+ std::printf("[bw] psram->psram %dus sram->psram %dus psram->sram %dus memset %dus (640KB)\n", int(t1 - t0), int(t2 - t1), int(t3 - t2), int(t4 - t3));
1895
+ }
1896
+ if (a) heap_caps_free(a);
1897
+ if (b) heap_caps_free(b);
1898
+ if (sram) heap_caps_free(sram);
1899
+ }
1900
+ #endif
1901
+ #if GEA_RGB_FRAME_TRACE
1902
+ xTaskCreatePinnedToCore([](void *) {
1903
+ std::uint32_t tail = 0;
1904
+ for (;;) {
1905
+ vTaskDelay(pdMS_TO_TICKS(200));
1906
+ const std::uint32_t head = g_traceHead;
1907
+ if (head - tail > 128) tail = head - 128;
1908
+ for (; tail != head; ++tail) {
1909
+ const FrameTrace t = g_trace[tail % 128];
1910
+ std::printf("[ft] %u dt=%d raster=%d copy=%d wait=%d pub=%d px=%d patch=%d p%u\n", unsigned(t.ms), int(t.dtUs), int(t.rasterUs), int(t.copyUs), int(t.waitUs), int(t.pubUs), int(t.px), int(t.patchPx), unsigned(t.path));
1911
+ }
1912
+ }
1913
+ }, "rgb_trace", 3072, nullptr, 1, nullptr, 0);
1636
1914
  #endif
1637
1915
  ESP_LOGI(kTag, "RGB panel ready %dx%d pclk=%d bounce_rows=%d fb=%p fb2=%p swap=%d isr_core=%d draw_fb=%p single_fb=%d row_bytes=%u fb_alignment64=%u vsync_restart=%d",
1638
1916
  kWidth,
@@ -2700,6 +2978,9 @@ extern "C" void gea_replay_stage_end()
2700
2978
  }
2701
2979
  }
2702
2980
 
2981
+ // The PIE vector unit is the S3's (Xtensa LX7); elsewhere (the ESP32-P4's
2982
+ // RISC-V cores) render.cpp's weak defaults keep the drawer on its scalar path.
2983
+ #if CONFIG_IDF_TARGET_ESP32S3
2703
2984
  namespace {
2704
2985
 
2705
2986
  // ── PIE SIMD over-blend (gea_pie_blend hooks) ────────────────────────────────
@@ -2795,6 +3076,7 @@ extern "C" void gea_pie_blend_span8(std::uint16_t *dst, const std::uint16_t *fgN
2795
3076
  pieBlendSpan8Native(dst, fgN, a5, count8, &cb2[0][0]);
2796
3077
  taskEXIT_CRITICAL(mux);
2797
3078
  }
3079
+ #endif // CONFIG_IDF_TARGET_ESP32S3
2798
3080
 
2799
3081
  extern "C" int gea_current_render_core()
2800
3082
  {
@@ -2845,6 +3127,16 @@ bool Display::init()
2845
3127
  return true;
2846
3128
  }
2847
3129
 
3130
+ // No zero-copy producer path yet: a camera (or video) producer composes into the
3131
+ // canvas and is flushed like any other rect. Declining here is what sends it there.
3132
+ bool Display::panelDirectTarget(int, int, int, int, uint16_t **, int *, int *, int *, int *, int *, int *, int *,
3133
+ int *)
3134
+ {
3135
+ return false;
3136
+ }
3137
+ void Display::flipPanelToBack() {}
3138
+ void Display::flipPanelToBack(uint16_t *) {}
3139
+
2848
3140
  bool Display::start()
2849
3141
  {
2850
3142
  if (!g_initialized && !init()) return false;
@@ -3400,6 +3692,7 @@ bool backRowReleased(std::uint32_t freeSeq, int row)
3400
3692
  }
3401
3693
  } // namespace
3402
3694
 
3695
+
3403
3696
  namespace {
3404
3697
  struct ScrollCopy {
3405
3698
  const std::uint16_t *src;
@@ -3497,6 +3790,27 @@ void Display::scrollRect(int x, int y, int w, int h, int dx, int dy)
3497
3790
  // copy at full speed from the other core starves the bounce ISR's refill of
3498
3791
  // the same bus, and the panel shows stale bounce rows (short glitch lines);
3499
3792
  // on the ISR's core the ISR simply preempts it.
3793
+ #if GEA_RGB_DMA_SCROLL
3794
+ // Full-width rows are contiguous memory: one transfer moves them all. A
3795
+ // sideways shift then also carries a few pixels from each row's end into
3796
+ // the next row's start, both inside the strip the shift exposes, which the
3797
+ // caller repaints after this returns.
3798
+ if (x0 == 0 && x1 == kWidth - 1) {
3799
+ const int ady = dy > 0 ? dy : -dy;
3800
+ const int srcRow = dy < 0 ? y0 + ady : y0, dstRow = dy > 0 ? y0 + dy : y0;
3801
+ const std::size_t len = dx != 0 ? (static_cast<std::size_t>(rh - 1) * kWidth) * sizeof(std::uint16_t) + bytes
3802
+ : static_cast<std::size_t>(rh - ady) * kWidth * sizeof(std::uint16_t);
3803
+ // It writes every row at once: the panel must be done with the buffer.
3804
+ waitBackBufferFree();
3805
+ if (dmaScrollCopy(&src[static_cast<std::size_t>(srcRow) * kWidth + sx0], &dst[static_cast<std::size_t>(dstRow) * kWidth + dxs], len)) {
3806
+ g_swapDiag.copyUs += esp_timer_get_time() - c0;
3807
+ ensureBackReady(&skip, 1);
3808
+ g_canvas.markDirty(x0, y0, x1, y1);
3809
+ markStale(x0, y0, x1, y1);
3810
+ return;
3811
+ }
3812
+ }
3813
+ #endif
3500
3814
  ScrollCopy job{src, dst, dxs, sx0, bytes, y0, y1, dy, g_freeSeq[g_drawIndex]};
3501
3815
  if (g_panelIsrCore >= 0 && g_panelIsrCore != static_cast<int>(xPortGetCoreID()) &&
3502
3816
  submitRenderWorkerJob(scrollCopyRows, &job, y0, y1)) {
@@ -3541,6 +3855,11 @@ extern "C" bool gea_display_scroll_rect_begin(int x, int y, int w, int h, int dx
3541
3855
  const std::uint32_t *const nextSig = g_nextSig;
3542
3856
  g_nextSig = nullptr;
3543
3857
  if (!g_swapFbs || g_backReady || onWorkerCore() || dy != 0 || dx == 0 || g_regionH > 0) return false;
3858
+ #if GEA_RGB_DMA_SCROLL
3859
+ // A full-width viewport: the DMA copy (Display::scrollRect) beats overlapping
3860
+ // the CPU copy with the strip repaint.
3861
+ if (x <= 0 && x + w >= kWidth && scrollDmaReady()) return false;
3862
+ #endif
3544
3863
  if (g_panelIsrCore < 0 || g_panelIsrCore == static_cast<int>(xPortGetCoreID())) return false;
3545
3864
  int x0 = x, y0 = y, x1 = x + w - 1, y1 = y + h - 1;
3546
3865
  int cx0, cy0, cx1, cy1;
@@ -0,0 +1,155 @@
1
+ // Elecrow CrowPanel Advanced ESP32-P4 board control: an STC8H1K17 MCU at 0x2F
2
+ // owns the slow lines the P4 has no pins for. Unlike the S3 CrowPanels' STC
3
+ // (0x30, single-byte commands), it takes ordinary register writes:
4
+ //
5
+ // 0x00+i read battery struct, one byte at a time:
6
+ // { u32 adc mV, u32 battery mV, u8 level %, u8 charge state, u8 led state }
7
+ // 0x10+n read input n (0: the SPI/UART function DIP switch)
8
+ // 0x18+n write output n: 0 TP_RST (not fitted), 1 CSI_RST (camera reset),
9
+ // 2 AUDIO_SD (amplifier shutdown, high = off), 3 LCD_BL_POWER
10
+ // (its switch is not fitted; the backlight is always powered)
11
+ // 0x20+n write PWM n, 0-100: 0 = backlight (MT9201 EN)
12
+ //
13
+ // Lines 0-3 are the outputs; line 4 is the backlight PWM, so a writePin on it
14
+ // switches the backlight and setLevel dims it.
15
+
16
+ #include "chip_bindings/expanders/elecrow_stc8h1k17.h"
17
+ #include "chip_bindings/expanders/io_expander.h"
18
+ #include "board.h"
19
+ #include "i2c.h"
20
+
21
+ #include <algorithm>
22
+
23
+ #include "driver/i2c_master.h"
24
+ #include "esp_log.h"
25
+ #include "freertos/FreeRTOS.h"
26
+ #include "freertos/semphr.h"
27
+ #include "freertos/task.h"
28
+
29
+ namespace gea::platform::esp32::chip_bindings::expanders {
30
+ namespace {
31
+
32
+ constexpr char kTag[] = "board_ctrl";
33
+ constexpr std::uint8_t kAddress = 0x2f;
34
+ constexpr std::uint8_t kRegBattery = 0x00;
35
+ constexpr std::uint8_t kRegInput = 0x10;
36
+ constexpr std::uint8_t kRegOutput = 0x18;
37
+ constexpr std::uint8_t kRegPwm = 0x20;
38
+ constexpr int kOutputCount = 4;
39
+ constexpr int kLineBacklight = 4;
40
+ constexpr int kTimeoutMs = 50;
41
+
42
+ class Stc8h1k17 final : public IoExpander {
43
+ public:
44
+ bool init() override {
45
+ Guard guard(lock_);
46
+ if (ready_) return true;
47
+ if (failed_) return false;
48
+ auto bus = gea::platform::i2c::Bus::primary();
49
+ if (!bus.available()) return false;
50
+ auto handle = static_cast<i2c_master_bus_handle_t>(bus.nativeHandle());
51
+ // The STC comes out of reset with the P4 (its NRST is CHIP_PU); give
52
+ // its firmware a moment to start answering.
53
+ vTaskDelay(pdMS_TO_TICKS(50));
54
+ esp_err_t err = ESP_FAIL;
55
+ for (int attempt = 0; attempt < 5 && err != ESP_OK; ++attempt) {
56
+ err = i2c_master_probe(handle, kAddress, kTimeoutMs);
57
+ if (err != ESP_OK) vTaskDelay(pdMS_TO_TICKS(20));
58
+ }
59
+ if (err != ESP_OK) {
60
+ ESP_LOGE(kTag, "STC8H1K17 board-control MCU did not answer at 0x%02x", kAddress);
61
+ failed_ = true;
62
+ return false;
63
+ }
64
+ i2c_device_config_t config{};
65
+ config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
66
+ config.device_address = kAddress;
67
+ config.scl_speed_hz = 100000;
68
+ if (i2c_master_bus_add_device(handle, &config, &device_) != ESP_OK) {
69
+ failed_ = true;
70
+ return false;
71
+ }
72
+ ready_ = true;
73
+ ESP_LOGI(kTag, "STC8H1K17 board-control MCU at 0x%02x", kAddress);
74
+ return true;
75
+ }
76
+
77
+ bool writePin(int pin, bool high) override {
78
+ if (!init()) return false;
79
+ Guard guard(lock_);
80
+ if (pin == kLineBacklight) return writeRegister(kRegPwm, high ? static_cast<std::uint8_t>(backlightPercent_) : 0);
81
+ if (pin < 0 || pin >= kOutputCount) return false;
82
+ return writeRegister(static_cast<std::uint8_t>(kRegOutput + pin), high ? 1 : 0);
83
+ }
84
+
85
+ bool readPin(int pin, bool &high) override {
86
+ if (!init()) return false;
87
+ Guard guard(lock_);
88
+ std::uint8_t value = 0;
89
+ if (pin != 0 || !readRegister(static_cast<std::uint8_t>(kRegInput + pin), value)) return false;
90
+ high = value != 0;
91
+ return true;
92
+ }
93
+
94
+ bool setLevel(int pin, int percent) override {
95
+ if (pin != kLineBacklight || !init()) return false;
96
+ Guard guard(lock_);
97
+ backlightPercent_ = std::clamp(percent, 0, 100);
98
+ return writeRegister(kRegPwm, static_cast<std::uint8_t>(backlightPercent_));
99
+ }
100
+
101
+ bool battery(Stc8h1k17Battery &out) {
102
+ if (!init()) return false;
103
+ Guard guard(lock_);
104
+ std::uint8_t raw[11]{};
105
+ for (std::uint8_t i = 0; i < sizeof(raw); ++i) {
106
+ if (!readRegister(static_cast<std::uint8_t>(kRegBattery + i), raw[i])) return false;
107
+ }
108
+ auto u32 = [&](int at) {
109
+ return static_cast<std::uint32_t>(raw[at]) | (static_cast<std::uint32_t>(raw[at + 1]) << 8) |
110
+ (static_cast<std::uint32_t>(raw[at + 2]) << 16) | (static_cast<std::uint32_t>(raw[at + 3]) << 24);
111
+ };
112
+ out.adcMillivolts = u32(0);
113
+ out.batteryMillivolts = u32(4);
114
+ out.levelPercent = raw[8];
115
+ out.chargeState = raw[9];
116
+ out.ledState = raw[10];
117
+ return true;
118
+ }
119
+
120
+ private:
121
+ struct Guard {
122
+ explicit Guard(SemaphoreHandle_t mutex) : mutex_(mutex) { xSemaphoreTakeRecursive(mutex_, portMAX_DELAY); }
123
+ ~Guard() { xSemaphoreGiveRecursive(mutex_); }
124
+ SemaphoreHandle_t mutex_;
125
+ };
126
+
127
+ bool writeRegister(std::uint8_t reg, std::uint8_t value) {
128
+ const std::uint8_t bytes[] = {reg, value};
129
+ return i2c_master_transmit(device_, bytes, 2, kTimeoutMs) == ESP_OK;
130
+ }
131
+
132
+ bool readRegister(std::uint8_t reg, std::uint8_t &value) {
133
+ return i2c_master_transmit_receive(device_, &reg, 1, &value, 1, kTimeoutMs) == ESP_OK;
134
+ }
135
+
136
+ StaticSemaphore_t mutexStorage_{};
137
+ SemaphoreHandle_t lock_ = xSemaphoreCreateRecursiveMutexStatic(&mutexStorage_);
138
+ i2c_master_dev_handle_t device_ = nullptr;
139
+ bool ready_ = false;
140
+ bool failed_ = false;
141
+ int backlightPercent_ = 100;
142
+ };
143
+
144
+ Stc8h1k17 &instance() {
145
+ static Stc8h1k17 driver;
146
+ return driver;
147
+ }
148
+
149
+ } // namespace
150
+
151
+ IoExpander &ioExpander() { return instance(); }
152
+ const char *ioExpanderDriverName() { return "elecrow-stc8h1k17"; }
153
+ bool stc8h1k17Battery(Stc8h1k17Battery &out) { return instance().battery(out); }
154
+
155
+ } // namespace gea::platform::esp32::chip_bindings::expanders
@@ -0,0 +1,20 @@
1
+ #pragma once
2
+
3
+ #include <cstdint>
4
+
5
+ // The battery gauge the CrowPanel Advanced P4's STC8H1K17 board-control MCU
6
+ // keeps (see elecrow_stc8h1k17.cpp for the register map).
7
+ namespace gea::platform::esp32::chip_bindings::expanders {
8
+
9
+ struct Stc8h1k17Battery {
10
+ std::uint32_t adcMillivolts = 0;
11
+ std::uint32_t batteryMillivolts = 0;
12
+ std::uint8_t levelPercent = 0;
13
+ // 0 idle, 1 charging, 2 full, 3 not charging, 4 error.
14
+ std::uint8_t chargeState = 0;
15
+ std::uint8_t ledState = 0;
16
+ };
17
+
18
+ bool stc8h1k17Battery(Stc8h1k17Battery &out);
19
+
20
+ } // namespace gea::platform::esp32::chip_bindings::expanders