@geastack/targets 0.1.79 → 0.1.81

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,141 @@
1
+ // ESP-IDF binding for the CH422G I/O expander: adapts the new i2c_master API
2
+ // to gea::chips::ch422g::Bus. The chip answers at four fixed addresses (one per
3
+ // function), so the binding holds one device handle per address on the primary
4
+ // bus. Boot-time levels and the pins' roles come from board.h.
5
+
6
+ #include "chip_bindings/expanders/io_expander.h"
7
+
8
+ #include "board.h"
9
+ #include "expanders/ch422g/ch422g.h"
10
+ #include "i2c.h"
11
+
12
+ #include "driver/i2c_master.h"
13
+ #include "esp_log.h"
14
+ #include "freertos/FreeRTOS.h"
15
+ #include "freertos/semphr.h"
16
+
17
+ namespace {
18
+
19
+ constexpr char kTag[] = "ch422g";
20
+ constexpr TickType_t kI2cTimeoutTicks = pdMS_TO_TICKS(100);
21
+
22
+ #if GEA_BOARD_HAS_EXPANDER
23
+ constexpr std::uint8_t kInitialOutputs = static_cast<std::uint8_t>(gea::platform::board::expander.initialOutputs);
24
+ #else
25
+ constexpr std::uint8_t kInitialOutputs = gea::chips::ch422g::kDefaultIoLevels;
26
+ #endif
27
+
28
+ class EspI2cCommandBus final : public gea::chips::ch422g::Bus {
29
+ public:
30
+ bool attach(i2c_master_bus_handle_t bus) {
31
+ return attachAddress(bus, gea::chips::ch422g::kAddressWriteSystem, system_) &&
32
+ attachAddress(bus, gea::chips::ch422g::kAddressWriteOpenDrain, openDrain_) &&
33
+ attachAddress(bus, gea::chips::ch422g::kAddressWriteIo, io_) &&
34
+ attachAddress(bus, gea::chips::ch422g::kAddressReadIo, read_);
35
+ }
36
+
37
+ bool write(std::uint8_t address, std::uint8_t value) override {
38
+ i2c_master_dev_handle_t device = handleFor(address);
39
+ return device && i2c_master_transmit(device, &value, 1, kI2cTimeoutTicks) == ESP_OK;
40
+ }
41
+
42
+ bool read(std::uint8_t address, std::uint8_t &value) override {
43
+ i2c_master_dev_handle_t device = handleFor(address);
44
+ return device && i2c_master_receive(device, &value, 1, kI2cTimeoutTicks) == ESP_OK;
45
+ }
46
+
47
+ private:
48
+ static bool attachAddress(i2c_master_bus_handle_t bus, std::uint8_t address, i2c_master_dev_handle_t &out) {
49
+ if (out) return true;
50
+ i2c_device_config_t config = {};
51
+ config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
52
+ config.device_address = address;
53
+ config.scl_speed_hz = gea::chips::ch422g::kI2cFrequencyHz;
54
+ return i2c_master_bus_add_device(bus, &config, &out) == ESP_OK;
55
+ }
56
+
57
+ i2c_master_dev_handle_t handleFor(std::uint8_t address) const {
58
+ if (address == gea::chips::ch422g::kAddressWriteSystem) return system_;
59
+ if (address == gea::chips::ch422g::kAddressWriteOpenDrain) return openDrain_;
60
+ if (address == gea::chips::ch422g::kAddressWriteIo) return io_;
61
+ if (address == gea::chips::ch422g::kAddressReadIo) return read_;
62
+ return nullptr;
63
+ }
64
+
65
+ i2c_master_dev_handle_t system_ = nullptr;
66
+ i2c_master_dev_handle_t openDrain_ = nullptr;
67
+ i2c_master_dev_handle_t io_ = nullptr;
68
+ i2c_master_dev_handle_t read_ = nullptr;
69
+ };
70
+
71
+ class Ch422gExpander final : public gea::platform::esp32::chip_bindings::expanders::IoExpander {
72
+ public:
73
+ bool init() override {
74
+ if (attempted_) return ready_;
75
+ attempted_ = true;
76
+ lock_ = xSemaphoreCreateMutex();
77
+
78
+ auto bus = gea::platform::i2c::Bus::primary();
79
+ if (!bus.available()) {
80
+ ESP_LOGE(kTag, "I2C bus not ready");
81
+ return false;
82
+ }
83
+ if (!bus_.attach(static_cast<i2c_master_bus_handle_t>(bus.nativeHandle()))) {
84
+ ESP_LOGE(kTag, "failed to add CH422G devices");
85
+ return false;
86
+ }
87
+ if (!driver_.configure(bus_, kInitialOutputs)) {
88
+ ESP_LOGE(kTag, "CH422G did not acknowledge");
89
+ return false;
90
+ }
91
+ ready_ = true;
92
+ ESP_LOGI(kTag, "CH422G ready (IO0-7 outputs=0x%02x)", static_cast<unsigned>(kInitialOutputs));
93
+ return true;
94
+ }
95
+
96
+ bool writePin(int pin, bool high) override {
97
+ if (!init()) return false;
98
+ Guard guard(lock_);
99
+ return driver_.setPin(bus_, pin, high);
100
+ }
101
+
102
+ bool readPin(int pin, bool &high) override {
103
+ if (!init()) return false;
104
+ if (pin < 0 || pin >= gea::chips::ch422g::kIoPinCount) return false;
105
+ Guard guard(lock_);
106
+ std::uint8_t levels = 0;
107
+ if (!driver_.readInputs(bus_, levels)) return false;
108
+ high = (levels >> pin) & 1u;
109
+ return true;
110
+ }
111
+
112
+ private:
113
+ struct Guard {
114
+ explicit Guard(SemaphoreHandle_t lock) : lock_(lock) {
115
+ if (lock_) xSemaphoreTake(lock_, portMAX_DELAY);
116
+ }
117
+ ~Guard() {
118
+ if (lock_) xSemaphoreGive(lock_);
119
+ }
120
+ SemaphoreHandle_t lock_;
121
+ };
122
+
123
+ EspI2cCommandBus bus_;
124
+ gea::chips::ch422g::Driver driver_;
125
+ SemaphoreHandle_t lock_ = nullptr;
126
+ bool attempted_ = false;
127
+ bool ready_ = false;
128
+ };
129
+
130
+ } // namespace
131
+
132
+ namespace gea::platform::esp32::chip_bindings::expanders {
133
+
134
+ IoExpander &ioExpander() {
135
+ static Ch422gExpander expander;
136
+ return expander;
137
+ }
138
+
139
+ const char *ioExpanderDriverName() { return "ch422g"; }
140
+
141
+ } // namespace gea::platform::esp32::chip_bindings::expanders
@@ -0,0 +1,29 @@
1
+ #pragma once
2
+
3
+ // The I/O expander a board routes slow control lines through: a backlight
4
+ // enable, a touch or panel reset, a card-select. Bindings that need one of
5
+ // those lines (rgb_panel.cpp, gt911.cpp) ask board.h which expander pin carries
6
+ // it and drive it through this interface; a board with no expander compiles
7
+ // them with GEA_BOARD_HAS_EXPANDER=0 and the calls fall away.
8
+ //
9
+ // Each board target links exactly one expander binding (ch422g, …) that
10
+ // provides these symbols. Pin numbers are the expander's own (0-based), not
11
+ // MCU GPIOs.
12
+ namespace gea::platform::esp32::chip_bindings::expanders
13
+ {
14
+
15
+ class IoExpander
16
+ {
17
+ public:
18
+ virtual ~IoExpander() = default;
19
+ // Idempotent: the first caller brings the chip up on the primary I2C bus;
20
+ // later callers see the cached result.
21
+ virtual bool init() = 0;
22
+ virtual bool writePin(int pin, bool high) = 0;
23
+ virtual bool readPin(int pin, bool &high) = 0;
24
+ };
25
+
26
+ IoExpander &ioExpander();
27
+ const char *ioExpanderDriverName();
28
+
29
+ } // namespace gea::platform::esp32::chip_bindings::expanders
@@ -0,0 +1,355 @@
1
+ // ESP-IDF binding for the GT911 touch controller: I2C transport, reset (over a
2
+ // GPIO or through the board's I/O expander), the INT line, and the task that
3
+ // turns reports into Touchscreen events. Unlike the FT3168 binding this file
4
+ // also answers the Touchscreen surface itself, so a composed board links it
5
+ // with no board-local glue.
6
+
7
+ #include "gt911_controller.h"
8
+
9
+ #include "board.h"
10
+ #include "i2c.h"
11
+ #if GEA_BOARD_HAS_EXPANDER
12
+ #include "chip_bindings/expanders/io_expander.h"
13
+ #endif
14
+
15
+ #include "driver/gpio.h"
16
+ #include "esp_attr.h"
17
+ #include "esp_log.h"
18
+ #include "esp_timer.h"
19
+ #include "freertos/FreeRTOS.h"
20
+ #include "freertos/task.h"
21
+
22
+ namespace {
23
+
24
+ constexpr char kTag[] = "gt911";
25
+ constexpr gpio_num_t kResetPin = gea::platform::board::touch.reset;
26
+ constexpr gpio_num_t kInterruptPin = gea::platform::board::touch.interrupt;
27
+ #if GEA_BOARD_HAS_EXPANDER
28
+ constexpr int kExpanderResetPin = gea::platform::board::expander.touchReset;
29
+ #else
30
+ constexpr int kExpanderResetPin = -1;
31
+ #endif
32
+ constexpr int kPollIntervalMs = 10;
33
+ // Same reasoning as the FT3168 binding: sample in the render's idle windows on
34
+ // CPU1 rather than preempting the raster worker; a board with real-time work
35
+ // on that core raises it.
36
+ #ifndef GEA_EMBEDDED_TOUCH_TASK_PRIORITY
37
+ #define GEA_EMBEDDED_TOUCH_TASK_PRIORITY 4
38
+ #endif
39
+ constexpr int kTaskPriority = GEA_EMBEDDED_TOUCH_TASK_PRIORITY;
40
+ constexpr TickType_t kI2cTimeoutTicks = pdMS_TO_TICKS(100);
41
+
42
+ volatile std::int64_t g_touchIntUs = 0;
43
+
44
+ } // namespace
45
+
46
+ namespace gea::platform::esp32::chip_bindings::gt911 {
47
+
48
+ bool EspI2cRegisterBus::attach(i2c_master_bus_handle_t bus, std::uint8_t address) {
49
+ if (device_) return true;
50
+ i2c_device_config_t config = {};
51
+ config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
52
+ config.device_address = address;
53
+ config.scl_speed_hz = gea::chips::gt911::kI2cFrequencyHz;
54
+ return i2c_master_bus_add_device(bus, &config, &device_) == ESP_OK;
55
+ }
56
+
57
+ void EspI2cRegisterBus::detach() {
58
+ if (!device_) return;
59
+ i2c_master_bus_rm_device(device_);
60
+ device_ = nullptr;
61
+ }
62
+
63
+ bool EspI2cRegisterBus::writeRegisters(std::uint16_t reg, const std::uint8_t *data, std::size_t length) {
64
+ if (!device_ || length > 30) return false;
65
+ std::uint8_t frame[32];
66
+ frame[0] = static_cast<std::uint8_t>(reg >> 8);
67
+ frame[1] = static_cast<std::uint8_t>(reg & 0xff);
68
+ for (std::size_t i = 0; i < length; i++) frame[2 + i] = data[i];
69
+ return i2c_master_transmit(device_, frame, 2 + length, kI2cTimeoutTicks) == ESP_OK;
70
+ }
71
+
72
+ bool EspI2cRegisterBus::readRegisters(std::uint16_t reg, std::uint8_t *data, std::size_t length) {
73
+ if (!device_) return false;
74
+ const std::uint8_t address[2] = {static_cast<std::uint8_t>(reg >> 8), static_cast<std::uint8_t>(reg & 0xff)};
75
+ return i2c_master_transmit_receive(device_, address, sizeof(address), data, length, kI2cTimeoutTicks) == ESP_OK;
76
+ }
77
+
78
+ TouchController &TouchController::instance() {
79
+ static TouchController controller;
80
+ return controller;
81
+ }
82
+
83
+ void TouchController::setObserver(Observer observer) { observer_ = observer; }
84
+ void TouchController::setPointerObserver(PointerObserver observer) { pointerObserver_ = observer; }
85
+
86
+ esp_err_t TouchController::begin() {
87
+ if (initialized_) return ESP_OK;
88
+
89
+ ESP_LOGI(kTag, "Initializing GT911 touch (RST=%d, expander RST=%d, INT=%d)", static_cast<int>(kResetPin),
90
+ kExpanderResetPin, static_cast<int>(kInterruptPin));
91
+
92
+ auto bus = gea::platform::i2c::Bus::primary();
93
+ if (!bus.available()) return ESP_ERR_INVALID_STATE;
94
+ bus_ = static_cast<i2c_master_bus_handle_t>(bus.nativeHandle());
95
+
96
+ esp_err_t err = resetChip();
97
+ if (err != ESP_OK) return err;
98
+
99
+ err = attachI2cDevice();
100
+ if (err != ESP_OK) return err;
101
+
102
+ err = startTask();
103
+ if (err != ESP_OK) return err;
104
+
105
+ err = attachInterrupt();
106
+ if (err != ESP_OK) return err;
107
+
108
+ initialized_ = true;
109
+ ESP_LOGI(kTag, "Touch controller ready (%s)", kInterruptPin >= 0 ? "interrupt-driven" : "polled");
110
+ return ESP_OK;
111
+ }
112
+
113
+ bool TouchController::read(TouchSample &sample) {
114
+ const bool touching = core_.read(device_, sample);
115
+ current_ = sample;
116
+ return touching;
117
+ }
118
+
119
+ TouchSample TouchController::cached() const { return current_; }
120
+
121
+ void TouchController::consumeLatestMove(int *x, int *y) {
122
+ latestMoveQueued_.store(false, std::memory_order_release);
123
+ if (x) *x = latestMove_.x;
124
+ if (y) *y = latestMove_.y;
125
+ }
126
+
127
+ // The GT911 latches its I2C address from the INT line as reset is released:
128
+ // low picks 0x5d, high picks 0x14. Driving INT low across the reset makes the
129
+ // address deterministic whichever way the board's pull goes; a board without
130
+ // INT wired just gets both addresses probed in attachI2cDevice.
131
+ esp_err_t TouchController::resetChip() {
132
+ if constexpr (kInterruptPin >= 0) {
133
+ gpio_config_t config = {};
134
+ config.pin_bit_mask = 1ULL << static_cast<unsigned>(kInterruptPin);
135
+ config.mode = GPIO_MODE_OUTPUT;
136
+ esp_err_t err = gpio_config(&config);
137
+ if (err != ESP_OK) return err;
138
+ gpio_set_level(kInterruptPin, 0);
139
+ }
140
+
141
+ bool pulsed = false;
142
+ if constexpr (kResetPin >= 0) {
143
+ gpio_config_t config = {};
144
+ config.pin_bit_mask = 1ULL << static_cast<unsigned>(kResetPin);
145
+ config.mode = GPIO_MODE_OUTPUT;
146
+ esp_err_t err = gpio_config(&config);
147
+ if (err != ESP_OK) return err;
148
+ gpio_set_level(kResetPin, 0);
149
+ vTaskDelay(pdMS_TO_TICKS(10));
150
+ gpio_set_level(kResetPin, 1);
151
+ pulsed = true;
152
+ }
153
+ #if GEA_BOARD_HAS_EXPANDER
154
+ if (!pulsed && kExpanderResetPin >= 0) {
155
+ auto &expander = gea::platform::esp32::chip_bindings::expanders::ioExpander();
156
+ if (!expander.writePin(kExpanderResetPin, false)) return ESP_FAIL;
157
+ vTaskDelay(pdMS_TO_TICKS(10));
158
+ if (!expander.writePin(kExpanderResetPin, true)) return ESP_FAIL;
159
+ pulsed = true;
160
+ }
161
+ #endif
162
+ if (!pulsed) ESP_LOGI(kTag, "no reset line for touch; relying on power-on reset");
163
+ // The controller needs ~50ms after reset before it answers; give it more
164
+ // on a cold boot where the panel power is still settling.
165
+ vTaskDelay(pdMS_TO_TICKS(pulsed ? 100 : 50));
166
+
167
+ if constexpr (kInterruptPin >= 0) {
168
+ // Hand INT back to the controller: input, no pull (the panel drives it).
169
+ gpio_config_t config = {};
170
+ config.pin_bit_mask = 1ULL << static_cast<unsigned>(kInterruptPin);
171
+ config.mode = GPIO_MODE_INPUT;
172
+ config.intr_type = GPIO_INTR_DISABLE;
173
+ esp_err_t err = gpio_config(&config);
174
+ if (err != ESP_OK) return err;
175
+ vTaskDelay(pdMS_TO_TICKS(10));
176
+ }
177
+ return ESP_OK;
178
+ }
179
+
180
+ esp_err_t TouchController::attachI2cDevice() {
181
+ const std::uint8_t addresses[] = {gea::chips::gt911::kI2cAddressPrimary, gea::chips::gt911::kI2cAddressSecondary};
182
+ for (std::uint8_t address : addresses) {
183
+ if (!device_.attach(bus_, address)) continue;
184
+ if (core_.configure(device_)) {
185
+ ESP_LOGI(kTag, "GT911 found at 0x%02x", address);
186
+ return ESP_OK;
187
+ }
188
+ device_.detach();
189
+ }
190
+ ESP_LOGE(kTag, "GT911 not found at 0x%02x or 0x%02x", gea::chips::gt911::kI2cAddressPrimary,
191
+ gea::chips::gt911::kI2cAddressSecondary);
192
+ return ESP_ERR_NOT_FOUND;
193
+ }
194
+
195
+ esp_err_t TouchController::startTask() {
196
+ task_ = xTaskCreateStaticPinnedToCore(taskEntry, "gt911-touch",
197
+ static_cast<std::uint32_t>(sizeof(taskStack_) / sizeof(taskStack_[0])), this,
198
+ kTaskPriority, taskStack_, &taskControlBlock_, 1);
199
+ if (task_) return ESP_OK;
200
+ ESP_LOGE(kTag, "Failed to start touch task");
201
+ return ESP_ERR_NO_MEM;
202
+ }
203
+
204
+ esp_err_t TouchController::attachInterrupt() {
205
+ if constexpr (kInterruptPin < 0) {
206
+ ESP_LOGI(kTag, "no interrupt GPIO for touch; polling instead");
207
+ return ESP_OK;
208
+ } else {
209
+ gpio_config_t config = {};
210
+ config.pin_bit_mask = 1ULL << static_cast<unsigned>(kInterruptPin);
211
+ config.mode = GPIO_MODE_INPUT;
212
+ // The GT911 pulses INT high for every report by default (the polarity
213
+ // is a config-register bit); waking on either edge covers both.
214
+ config.intr_type = GPIO_INTR_ANYEDGE;
215
+ esp_err_t err = gpio_config(&config);
216
+ if (err != ESP_OK) return err;
217
+
218
+ err = gpio_install_isr_service(0);
219
+ if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) return err;
220
+
221
+ return gpio_isr_handler_add(kInterruptPin, interruptEntry, this);
222
+ }
223
+ }
224
+
225
+ void IRAM_ATTR TouchController::interruptEntry(void *arg) {
226
+ auto *self = static_cast<TouchController *>(arg);
227
+ if (self) self->notifyTaskFromInterrupt();
228
+ }
229
+
230
+ void IRAM_ATTR TouchController::notifyTaskFromInterrupt() {
231
+ g_touchIntUs = esp_timer_get_time();
232
+ BaseType_t woken = pdFALSE;
233
+ if (task_) vTaskNotifyGiveFromISR(task_, &woken);
234
+ portYIELD_FROM_ISR(woken);
235
+ }
236
+
237
+ void TouchController::taskEntry(void *arg) { static_cast<TouchController *>(arg)->pollLoop(); }
238
+
239
+ void TouchController::pollLoop() {
240
+ // Per-finger slot tracking (the same shape as the FT3168 and CST9217
241
+ // bindings): each slot emits its own Down/Move/Up tagged with its pointerId,
242
+ // so a second finger reaches pinch-zoom. Slot 0 is the primary pointer.
243
+ bool slotActive[kMaxFingers] = {false, false};
244
+ int slotX[kMaxFingers] = {0, 0};
245
+ int slotY[kMaxFingers] = {0, 0};
246
+ bool anyActive = false;
247
+
248
+ while (true) {
249
+ constexpr bool kInterruptDriven = kInterruptPin >= 0;
250
+ const TickType_t wait = (anyActive || !kInterruptDriven) ? pdMS_TO_TICKS(kPollIntervalMs) : portMAX_DELAY;
251
+ ulTaskNotifyTake(pdTRUE, wait);
252
+
253
+ gea::chips::gt911::MultiTouchSample multi;
254
+ core_.readMulti(device_, multi);
255
+
256
+ current_ = multi.count > 0 ? TouchSample{true, multi.x[0], multi.y[0]}
257
+ : TouchSample{false, current_.x, current_.y};
258
+
259
+ for (int slot = 0; slot < kMaxFingers; slot++) {
260
+ const bool nowActive = slot < multi.count;
261
+ const int x = nowActive ? multi.x[slot] : slotX[slot];
262
+ const int y = nowActive ? multi.y[slot] : slotY[slot];
263
+
264
+ if (nowActive && !slotActive[slot]) {
265
+ if (slot == 0) latestMove_ = TouchSample{true, x, y};
266
+ notifyPointer(Phase::Down, true, x, y, slot);
267
+ } else if (nowActive && slotActive[slot] && (x != slotX[slot] || y != slotY[slot])) {
268
+ if (slot == 0) {
269
+ latestMove_ = TouchSample{true, x, y};
270
+ if (!latestMoveQueued_.exchange(true, std::memory_order_acq_rel)) {
271
+ notifyPointer(Phase::Move, true, x, y, slot);
272
+ }
273
+ } else {
274
+ notifyPointer(Phase::Move, true, x, y, slot);
275
+ }
276
+ } else if (!nowActive && slotActive[slot]) {
277
+ notifyPointer(Phase::Up, false, x, y, slot);
278
+ }
279
+
280
+ if (nowActive) {
281
+ slotX[slot] = x;
282
+ slotY[slot] = y;
283
+ }
284
+ slotActive[slot] = nowActive;
285
+ }
286
+ anyActive = multi.count > 0;
287
+ }
288
+ }
289
+
290
+ void TouchController::notifyPointer(Phase phase, bool touching, int x, int y, int pointerId) {
291
+ if (pointerObserver_) {
292
+ pointerObserver_(phase, touching, x, y, pointerId);
293
+ } else if (observer_ && pointerId == 0) {
294
+ observer_(phase, touching, x, y);
295
+ }
296
+ }
297
+
298
+ void TouchController::notifyMove(const TouchSample &sample) {
299
+ latestMove_ = sample;
300
+ if (latestMoveQueued_.exchange(true, std::memory_order_acq_rel)) return;
301
+ notifyPointer(Phase::Move, true, sample.x, sample.y, 0);
302
+ }
303
+
304
+ void TouchController::injectEvent(Phase phase, bool touching, int x, int y) {
305
+ TouchSample sample;
306
+ sample.x = x;
307
+ sample.y = y;
308
+ sample.touching = touching;
309
+ current_ = sample;
310
+ if (phase == Phase::Move) notifyMove(sample);
311
+ else notifyPointer(phase, touching, x, y, 0);
312
+ }
313
+
314
+ } // namespace gea::platform::esp32::chip_bindings::gt911
315
+
316
+ // The Touchscreen surface, answered here so a composed board links the GT911
317
+ // without a board-local touch.cpp.
318
+ namespace {
319
+
320
+ gea::platform::esp32::chip_bindings::gt911::TouchController &touchController() {
321
+ return gea::platform::esp32::chip_bindings::gt911::TouchController::instance();
322
+ }
323
+
324
+ } // namespace
325
+
326
+ void gea::platform::touch::Touchscreen::setObserver(Observer observer) { touchController().setObserver(observer); }
327
+
328
+ // Strong override of the runtime's weak no-op: the GT911 reports several
329
+ // fingers, so every contact reaches the app.
330
+ void gea::platform::touch::Touchscreen::setPointerObserver(PointerObserver observer) {
331
+ touchController().setPointerObserver(observer);
332
+ }
333
+
334
+ bool gea::platform::touch::Touchscreen::init() { return touchController().begin() == ESP_OK; }
335
+
336
+ int gea::platform::touch::Touchscreen::read(int *x, int *y) {
337
+ gea::chips::gt911::TouchSample sample;
338
+ const bool touching = touchController().read(sample);
339
+ if (x) *x = sample.x;
340
+ if (y) *y = sample.y;
341
+ return touching ? 1 : 0;
342
+ }
343
+
344
+ int gea::platform::touch::Touchscreen::readCached(int *x, int *y) {
345
+ const auto sample = touchController().cached();
346
+ if (x) *x = sample.x;
347
+ if (y) *y = sample.y;
348
+ return sample.touching ? 1 : 0;
349
+ }
350
+
351
+ void gea::platform::touch::Touchscreen::consumeLatestMove(int *x, int *y) { touchController().consumeLatestMove(x, y); }
352
+
353
+ void gea::platform::touch::Touchscreen::injectEvent(Phase phase, bool touching, int x, int y) {
354
+ touchController().injectEvent(phase, touching, x, y);
355
+ }
@@ -0,0 +1,84 @@
1
+ #pragma once
2
+
3
+ #include "touch/gt911/gt911.h"
4
+ #include "touch.h"
5
+
6
+ #include <cstddef>
7
+ #include <cstdint>
8
+ #include <atomic>
9
+
10
+ #include "driver/gpio.h"
11
+ #include "driver/i2c_master.h"
12
+ #include "esp_err.h"
13
+ #include "freertos/FreeRTOS.h"
14
+ #include "freertos/task.h"
15
+
16
+ namespace gea::platform::esp32::chip_bindings::gt911 {
17
+
18
+ using TouchSample = gea::chips::gt911::TouchSample;
19
+
20
+ // GT911 registers are 16 bits wide; the address goes out big-endian ahead of
21
+ // the data in one transaction.
22
+ class EspI2cRegisterBus final : public gea::chips::gt911::RegisterBus {
23
+ public:
24
+ bool attach(i2c_master_bus_handle_t bus, std::uint8_t address);
25
+ void detach();
26
+ bool attached() const { return device_ != nullptr; }
27
+ bool writeRegisters(std::uint16_t reg, const std::uint8_t *data, std::size_t length) override;
28
+ bool readRegisters(std::uint16_t reg, std::uint8_t *data, std::size_t length) override;
29
+
30
+ private:
31
+ i2c_master_dev_handle_t device_ = nullptr;
32
+ };
33
+
34
+ class TouchController {
35
+ public:
36
+ using Observer = gea::platform::touch::Touchscreen::Observer;
37
+ using PointerObserver = gea::platform::touch::Touchscreen::PointerObserver;
38
+ using Phase = gea::platform::touch::Phase;
39
+
40
+ TouchController(const TouchController &) = delete;
41
+ TouchController &operator=(const TouchController &) = delete;
42
+
43
+ static TouchController &instance();
44
+
45
+ void setObserver(Observer observer);
46
+ void setPointerObserver(PointerObserver observer);
47
+ esp_err_t begin();
48
+ bool read(TouchSample &sample);
49
+ TouchSample cached() const;
50
+ void consumeLatestMove(int *x, int *y);
51
+ void injectEvent(Phase phase, bool touching, int x, int y);
52
+
53
+ private:
54
+ TouchController() = default;
55
+
56
+ esp_err_t resetChip();
57
+ esp_err_t attachI2cDevice();
58
+ esp_err_t startTask();
59
+ esp_err_t attachInterrupt();
60
+ static void interruptEntry(void *arg);
61
+ void notifyTaskFromInterrupt();
62
+ static void taskEntry(void *arg);
63
+ void pollLoop();
64
+ void notifyPointer(Phase phase, bool touching, int x, int y, int pointerId);
65
+ void notifyMove(const TouchSample &sample);
66
+
67
+ static constexpr std::size_t kTaskStackWords = 4096;
68
+ static constexpr int kMaxFingers = gea::chips::gt911::kMaxTouchPoints;
69
+
70
+ bool initialized_ = false;
71
+ i2c_master_bus_handle_t bus_ = nullptr;
72
+ EspI2cRegisterBus device_;
73
+ gea::chips::gt911::ControllerCore core_;
74
+ TaskHandle_t task_ = nullptr;
75
+ StaticTask_t taskControlBlock_ = {};
76
+ StackType_t taskStack_[kTaskStackWords] = {};
77
+ TouchSample current_ = {};
78
+ TouchSample latestMove_ = {};
79
+ std::atomic<bool> latestMoveQueued_{false};
80
+ Observer observer_ = nullptr;
81
+ PointerObserver pointerObserver_ = nullptr;
82
+ };
83
+
84
+ } // namespace gea::platform::esp32::chip_bindings::gt911
@@ -4,21 +4,20 @@ Patches applied to ESP-IDF to unlock features the stock SDK gates off.
4
4
  Each patch is a standalone `git format-patch` output — applies cleanly with
5
5
  `git am` on top of the SDK commit it targets.
6
6
 
7
- ## NOT needed on ESP-IDF 6.0+ (the supported version)
7
+ ## Not needed on ESP-IDF 6.0+ (the supported version)
8
8
 
9
- As of ESP-IDF **6.0**, both patches below are **obsolete — Espressif
10
- upstreamed PSRAM SPI DMA**, verified against v6.0.1:
9
+ As of ESP-IDF 6.0, both patches below are obsolete: Espressif added PSRAM
10
+ SPI DMA upstream (checked against v6.0.1).
11
11
 
12
12
  - `components/esp_driver_spi/src/gpspi/spi_common.c` now ships
13
- `.access_ext_mem = true` in the GDMA transfer config by default (this was
14
- the entire point of patch 0002).
13
+ `.access_ext_mem = true` in the GDMA transfer config by default (the main
14
+ change in patch 0002).
15
15
  - `components/esp_driver_spi/src/gpspi/spi_master.c` no longer gates TX
16
16
  buffers on `esp_ptr_dma_capable()` / no internal bounce-buffer path, so
17
17
  patch 0001 (widening `esp_ptr_dma_capable`) has no effect — on 6.0 that
18
18
  function is only read for a diagnostic log in the gea display backend.
19
19
 
20
- So on 6.0 the PSRAM framebuffer DMAs to the QSPI panel **out of the box, no
21
- SDK patching**. The gea backend's `esp_cache_msync(..., C2M)` before each
20
+ On 6.0 the PSRAM framebuffer DMAs to the QSPI panel without SDK patches. The gea backend's `esp_cache_msync(..., C2M)` before each
22
21
  flush is still required and is present (targets/esp32/display.cpp).
23
22
 
24
23
  One residual tuning difference: 6.0 defaults the SPI GDMA
@@ -109,8 +108,8 @@ gdma_transfer_config_t trans_cfg = {
109
108
  };
110
109
  ```
111
110
 
112
- ESP-IDF themselves left a `TODO` to add PSRAM support. Without the flag,
113
- GDMA happily queues descriptors that reference PSRAM addresses but reads
111
+ ESP-IDF 5.5 left a `TODO` to add PSRAM support. Without the flag,
112
+ GDMA queues descriptors that reference PSRAM addresses but reads
114
113
  garbage from the bus when it actually tries to fetch — you see horizontal
115
114
  bands of solid colour on the LCD instead of the framebuffer contents.
116
115
  Setting the flag to `true` enables the GDMA hardware path that resolves
@@ -121,7 +120,6 @@ since the GDMA channel is now configured to support both. The cost is in
121
120
  descriptor setup, not transfer rate. No correctness impact for non-PSRAM
122
121
  sources.
123
122
 
124
- **Upstream:** the TODO suggests Espressif will eventually do this themselves;
125
- when they do, this patch becomes redundant. Until then, it's a local
126
- override. Keep applied as long as you are building with ESP-IDF ≤ 5.5.x
127
- and using PSRAM framebuffers.
123
+ **Upstream:** ESP-IDF 6.0 includes this change (see above). Keep the patch
124
+ applied only when building with ESP-IDF ≤ 5.5.x and using PSRAM
125
+ framebuffers.
@@ -107,8 +107,11 @@ file(GLOB _GEA_FONT_ASSETS
107
107
  # GEA_EMBEDDED_APP_DIR/ENTRY/RUNTIME/NATIVE_SOURCE_RELS variables come from there.
108
108
 
109
109
  set(GEA_EMBEDDED_APP_NATIVE_SOURCES "")
110
+ set(GEA_EMBEDDED_APP_NATIVE_INCLUDE_DIRS "")
110
111
  foreach(_GEA_EMBEDDED_APP_NATIVE_SOURCE_REL IN LISTS GEA_EMBEDDED_APP_NATIVE_SOURCE_RELS)
111
112
  list(APPEND GEA_EMBEDDED_APP_NATIVE_SOURCES "${GEA_EMBEDDED_APP_DIR}/${_GEA_EMBEDDED_APP_NATIVE_SOURCE_REL}")
113
+ get_filename_component(_GEA_APP_NATIVE_DIR "${GEA_EMBEDDED_APP_DIR}/${_GEA_EMBEDDED_APP_NATIVE_SOURCE_REL}" DIRECTORY)
114
+ list(APPEND GEA_EMBEDDED_APP_NATIVE_INCLUDE_DIRS "${_GEA_APP_NATIVE_DIR}")
112
115
  endforeach()
113
116
 
114
117
  set(JS_APP_SRC "${GEA_EMBEDDED_APP_DIR}/${GEA_EMBEDDED_APP_ENTRY}")
@@ -336,6 +339,7 @@ idf_component_register(
336
339
  "include"
337
340
  "${GEA_EMBEDDED_ROOT}/targets/esp32"
338
341
  ${GEA_FW_INCLUDE_DIRS}
342
+ ${GEA_EMBEDDED_APP_NATIVE_INCLUDE_DIRS}
339
343
  "${GEA_CHIPS}"
340
344
  ${GENERATED_APP_DIR}
341
345
  ${CMAKE_BINARY_DIR}
@@ -373,6 +377,12 @@ idf_component_register(
373
377
  LDFRAGMENTS
374
378
  )
375
379
 
380
+ # Generated application references may pull the camera backend from main only
381
+ # during the final framework/main rescan, after esp_video's archive was scanned.
382
+ # Seed its C entry points early so static linking resolves them in normal order.
383
+ target_link_options(${COMPONENT_LIB} INTERFACE
384
+ "-Wl,-u,esp_video_init" "-Wl,-u,mmap" "-Wl,-u,munmap")
385
+
376
386
  if(TARGET idf::espressif__esp_ipa AND TARGET idf::esp_hal_cam)
377
387
  idf_component_get_property(_GEA_ESP_IPA_LIB espressif__esp_ipa COMPONENT_LIB)
378
388
  target_link_libraries(${_GEA_ESP_IPA_LIB} PUBLIC idf::esp_hal_cam)