@geastack/targets 0.1.109 → 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.
@@ -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
@@ -0,0 +1,35 @@
1
+ #include "power.h"
2
+
3
+ #include "chip_bindings/expanders/elecrow_stc8h1k17.h"
4
+
5
+ // The battery (PH2.0 J3, TP4059 charger) is gauged by the STC8H1K17
6
+ // board-control MCU: it divides VBAT into its ADC and reads the charger's
7
+ // CHRG/DONE pins, and the P4 reads the result over I2C.
8
+ namespace gea::platform::power {
9
+
10
+ namespace {
11
+ namespace board_control = gea::platform::esp32::chip_bindings::expanders;
12
+ }
13
+
14
+ bool Power::init()
15
+ {
16
+ board_control::Stc8h1k17Battery battery;
17
+ return board_control::stc8h1k17Battery(battery);
18
+ }
19
+
20
+ int Power::batteryPercent()
21
+ {
22
+ board_control::Stc8h1k17Battery battery;
23
+ if (!board_control::stc8h1k17Battery(battery)) return -1;
24
+ // No cell fitted reads as (almost) no voltage: report "no battery".
25
+ if (battery.batteryMillivolts < 2500) return -1;
26
+ return battery.levelPercent > 100 ? 100 : battery.levelPercent;
27
+ }
28
+
29
+ bool Power::charging()
30
+ {
31
+ board_control::Stc8h1k17Battery battery;
32
+ return board_control::stc8h1k17Battery(battery) && battery.chargeState == 1;
33
+ }
34
+
35
+ } // namespace gea::platform::power
@@ -2,6 +2,9 @@
2
2
 
3
3
  #include "freertos/FreeRTOS.h"
4
4
  #include "freertos/semphr.h"
5
+ #include "freertos/task.h"
6
+
7
+ #include <atomic>
5
8
 
6
9
  namespace gea::framework::services {
7
10
 
@@ -22,18 +25,33 @@ public:
22
25
  return mutex_ != nullptr;
23
26
  }
24
27
 
28
+ // The frame task holds the lock for a whole frame and takes it again
29
+ // straight after releasing it. A FreeRTOS mutex does not hand itself to a
30
+ // waiter on give, so while the app renders continuously (a blinking caret is
31
+ // enough) a lower-priority waiter -- the GEADEV console reading the tree or
32
+ // the screen -- went ~30 s without it. The task that just released steps
33
+ // aside for one tick when someone is waiting; nobody waits on a normal frame.
25
34
  void lock()
26
35
  {
27
- if (mutex_) xSemaphoreTake(mutex_, portMAX_DELAY);
36
+ if (!mutex_) return;
37
+ if (waiting_.load(std::memory_order_acquire) > 0 && lastReleaser_ == xTaskGetCurrentTaskHandle())
38
+ vTaskDelay(1);
39
+ waiting_.fetch_add(1, std::memory_order_acq_rel);
40
+ xSemaphoreTake(mutex_, portMAX_DELAY);
41
+ waiting_.fetch_sub(1, std::memory_order_acq_rel);
28
42
  }
29
43
 
30
44
  void unlock()
31
45
  {
32
- if (mutex_) xSemaphoreGive(mutex_);
46
+ if (!mutex_) return;
47
+ lastReleaser_ = xTaskGetCurrentTaskHandle();
48
+ xSemaphoreGive(mutex_);
33
49
  }
34
50
 
35
51
  private:
36
52
  SemaphoreHandle_t mutex_ = nullptr;
53
+ std::atomic<int> waiting_{0};
54
+ TaskHandle_t lastReleaser_ = nullptr;
37
55
  };
38
56
 
39
57
  } // namespace
@@ -253,12 +253,26 @@ private:
253
253
  void flushLine()
254
254
  {
255
255
  if (lineLength_ <= 0) return;
256
+ // One line at a time under the stdout lock: other output can fall between
257
+ // data lines (the host skips it) but never inside one. Holding the lock
258
+ // for the whole ~10 s stream blocked every task that logged meanwhile,
259
+ // and the task watchdog's register dump then landed inside the data.
260
+ flockfile(stdout);
256
261
  std::printf("GEADEV:DATA ");
257
262
  std::fwrite(line_, 1, static_cast<std::size_t>(lineLength_), stdout);
258
263
  std::printf("\n");
264
+ funlockfile(stdout);
259
265
  lineLength_ = 0;
266
+ // The console write can busy-wait on the UART FIFO; over a ~10 s frame
267
+ // that starved this core's idle task and tripped the task watchdog,
268
+ // whose register dump then landed in the data. Let it run now and then.
269
+ if (++linesSinceYield_ >= 32) {
270
+ linesSinceYield_ = 0;
271
+ vTaskDelay(1);
272
+ }
260
273
  }
261
274
 
275
+ int linesSinceYield_ = 0;
262
276
  static constexpr int kLineLength = 76;
263
277
  static constexpr char kAlphabet[] =
264
278
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -375,8 +389,28 @@ bool captureSnapshotPacked(std::uint8_t *snapshot, int byteCapacity, int *width,
375
389
  }
376
390
  #endif // GEA_PIXEL_STORAGE_PACKED
377
391
 
392
+ // Silences ESP log output for its lifetime: a log line printed into the middle
393
+ // of a streamed reply (a screenshot's base64, the debugger protocol) corrupts it.
394
+ class ScopedLogSilence {
395
+ public:
396
+ ScopedLogSilence()
397
+ : previous_(esp_log_level_get(nullptr))
398
+ {
399
+ esp_log_level_set("*", ESP_LOG_NONE);
400
+ }
401
+
402
+ ~ScopedLogSilence()
403
+ {
404
+ esp_log_level_set("*", previous_);
405
+ }
406
+
407
+ private:
408
+ esp_log_level_t previous_;
409
+ };
410
+
378
411
  void writeScreenshot()
379
412
  {
413
+ ScopedLogSilence silence;
380
414
  int width = 0;
381
415
  int height = 0;
382
416
  char appIdBuffer[96];
@@ -411,7 +445,6 @@ void writeScreenshot()
411
445
  return;
412
446
  }
413
447
 
414
- flockfile(stdout);
415
448
  std::printf("GEADEV:SCREENSHOT BEGIN width=%d height=%d encoding=rgb565-rle-v1 app=%s\n",
416
449
  width,
417
450
  height,
@@ -438,7 +471,6 @@ void writeScreenshot()
438
471
  writer.finish();
439
472
  std::printf("GEADEV:SCREENSHOT END\n");
440
473
  std::fflush(stdout);
441
- funlockfile(stdout);
442
474
 
443
475
  if (heapCapsAllocated) heap_caps_free(snapshot);
444
476
  else std::free(snapshot);
@@ -463,7 +495,6 @@ void writeScreenshot()
463
495
  const char *liveAppId = gea::framework::apps::AppManager::currentId();
464
496
  std::snprintf(appIdBuffer, sizeof(appIdBuffer), "%s", liveAppId ? liveAppId : "");
465
497
  gea::framework::services::AppState::unlock();
466
- flockfile(stdout);
467
498
  std::printf("GEADEV:SCREENSHOT BEGIN width=%d height=%d encoding=rgb565-rle-v1 app=%s\n", width, height, appIdBuffer);
468
499
  Base64Writer writer;
469
500
  using gea::framework::graphics::pixel::toRgb565;
@@ -483,7 +514,6 @@ void writeScreenshot()
483
514
  writer.finish();
484
515
  std::printf("GEADEV:SCREENSHOT END\n");
485
516
  std::fflush(stdout);
486
- funlockfile(stdout);
487
517
  return;
488
518
  }
489
519
 
@@ -497,7 +527,6 @@ void writeScreenshot()
497
527
  return;
498
528
  }
499
529
 
500
- flockfile(stdout);
501
530
  std::printf("GEADEV:SCREENSHOT BEGIN width=%d height=%d encoding=rgb565-rle-v1 app=%s\n",
502
531
  width,
503
532
  height,
@@ -522,7 +551,6 @@ void writeScreenshot()
522
551
  writer.finish();
523
552
  std::printf("GEADEV:SCREENSHOT END\n");
524
553
  std::fflush(stdout);
525
- funlockfile(stdout);
526
554
 
527
555
  if (heapCapsAllocated) heap_caps_free(snapshot);
528
556
  else std::free(snapshot);
@@ -530,23 +558,6 @@ void writeScreenshot()
530
558
  }
531
559
 
532
560
  #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
533
- class ScopedLogSilence {
534
- public:
535
- ScopedLogSilence()
536
- : previous_(esp_log_level_get(nullptr))
537
- {
538
- esp_log_level_set("*", ESP_LOG_NONE);
539
- }
540
-
541
- ~ScopedLogSilence()
542
- {
543
- esp_log_level_set("*", previous_);
544
- }
545
-
546
- private:
547
- esp_log_level_t previous_;
548
- };
549
-
550
561
  bool usbWriteAll(const void *data, std::size_t size)
551
562
  {
552
563
  const auto *bytes = static_cast<const std::uint8_t *>(data);
@@ -847,6 +858,16 @@ void handleI2cScan()
847
858
  std::printf("GEADEV:I2CSCAN END count=%d\n", count);
848
859
  }
849
860
 
861
+ // A string as one whitespace-free token: printable ASCII as is, everything
862
+ // else (space, backslash, UTF-8 bytes) as \xHH.
863
+ void printEscaped(const char *text)
864
+ {
865
+ for (const unsigned char *c = reinterpret_cast<const unsigned char *>(text); *c; ++c) {
866
+ if (*c > 0x20 && *c < 0x7f && *c != '\\') std::putchar(*c);
867
+ else std::printf("\\x%02X", *c);
868
+ }
869
+ }
870
+
850
871
  void handleNode(char *&cursor)
851
872
  {
852
873
  const char *className = nextToken(cursor);
@@ -945,6 +966,21 @@ void handleNode(char *&cursor)
945
966
  textCommand ? textCommand->bh : 0,
946
967
  textCommand ? textCommand->text.fontId : -1,
947
968
  textCommand ? static_cast<double>(textCommand->text.scale) : 0.0);
969
+ // An input's value and the text it draws (bullets on a password), so a
970
+ // host can drive the on-screen keyboard and check what each key did.
971
+ const char *value = tree.getAttribute(nodeId, "value");
972
+ if (value || (textCommand && textCommand->text.text)) {
973
+ std::printf("GEADEV:NODETEXT id=%d focused=%d caret=%d value=", nodeId,
974
+ tree.activeInputId() == nodeId ? 1 : 0, tree.activeInputId() == nodeId ? tree.activeInputCaret() : -1);
975
+ printEscaped(value ? value : "");
976
+ std::printf(" text=");
977
+ printEscaped(textCommand && textCommand->text.text ? textCommand->text.text : "");
978
+ std::printf("\n");
979
+ int revealFrom = 0, revealTo = 0;
980
+ if (tree.inputPasswordReveal(nodeId, &revealFrom, &revealTo))
981
+ std::printf("GEADEV:NODEREVEAL id=%d from=%d to=%d frame_ms=%d now_ms=%d\n", nodeId, revealFrom, revealTo,
982
+ tree.lastFrameMs(), static_cast<int>(esp_timer_get_time() / 1000));
983
+ }
948
984
  printed++;
949
985
  }
950
986
  gea::framework::services::AppState::unlock();
@@ -0,0 +1,18 @@
1
+ cmake_minimum_required(VERSION 3.16)
2
+
3
+ # Resolve exactly what main/idf_component.yml pins. The component manager's
4
+ # "is there a newer version" pass reads registry metadata for releases this
5
+ # board never uses, and newer esp_hosted releases gate their USB-host transports
6
+ # on `$CONFIG{ESP_HOSTED_HOST_USB_*} == 1` rules that no Kconfig in the build
7
+ # defines -- which stops every fresh dependency solve at configure time.
8
+ set(ENV{IDF_COMPONENT_CHECK_NEW_VERSION} 0)
9
+
10
+ set(EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/../esp32/components")
11
+ include("${CMAKE_CURRENT_LIST_DIR}/../esp32/app_project.cmake")
12
+ set(GEA_EMBEDDED_APP "tic-tac-toe" CACHE STRING "Example app to build (must match a package.json gea.id)")
13
+ set(PROJECT_VER "${GEA_EMBEDDED_APP}" CACHE STRING "Firmware app id" FORCE)
14
+
15
+ include($ENV{IDF_PATH}/tools/cmake/project.cmake)
16
+ idf_build_set_property(MINIMAL_BUILD ON)
17
+ gea_solve_dependencies_in_build_dir()
18
+ project(gea_embedded)