SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
TouchDrvCST92xx.cpp
Go to the documentation of this file.
1
29#include "../SensorBuildOpt.h"
30#if !SENSORLIB_EXCLUDE_TOUCH_CST92XX
31
32#include "TouchDrvCST92xx.hpp"
33
35{
36}
37
39{
40 uint8_t numPoints = 0;
41 uint8_t buffer[MAX_FINGER_NUM * 5 + 5] = {0};
42 uint8_t write_buffer[4] = {0};
43
44 // Clear cached touch points
46
47 // Write read command
48 write_buffer[0] = sensorlib::_highByte(REG_READ);
49 write_buffer[1] = sensorlib::_lowByte(REG_READ);
50 addrToBeBuf(REG_READ, buffer);
51
52 if (writeThenRead(buffer, 2, buffer, sizeof(buffer)) == 0) {
53
54 // Write read ACK
55 write_buffer[0] = sensorlib::_highByte(REG_READ);
56 write_buffer[1] = sensorlib::_lowByte(REG_READ);
57 write_buffer[2] = CST92XX_ACK;
58
59 if (writeBuff(write_buffer, 3) != 0) {
60 return _touchPoints;
61 }
62
63 if (buffer[0] == CST92XX_ACK || buffer[6] != CST92XX_ACK || buffer[0] == 0x00) {
64 return _touchPoints;
65 }
66
67 // Check cover screen gesture
68 if (buffer[4] & 0xF0) {
69 if ((buffer[4] >> 7) == 0x01) {
70 if (_HButtonCallback) {
72 }
73 // Cover screen gesture detected, return zero points
74 return _touchPoints;
75 }
76 }
77
78 numPoints = (buffer[5] & 0x7F); // Get number of touch points
79 if (numPoints > MAX_FINGER_NUM || numPoints == 0) {
80 return _touchPoints;
81 }
82
83 for (uint8_t i = 0; i < numPoints; ++i) {
84 uint8_t *pdat = buffer + (i * 5) + (i == 0 ? 0 : 2);
85 const uint8_t id = (pdat[0] >> 4);
86 const uint8_t event = (pdat[0] & 0x0F);
87 const uint16_t x = ((pdat[1] << 4) | (pdat[3] >> 4));
88 const uint16_t y = ((pdat[2] << 4) | (pdat[3] & 0x0F));
89 if (event == 0x06 && id < MAX_FINGER_NUM) {
90 _touchPoints.addPoint(x, y, 0, id, event);
91 }
92 }
93
94 if (_touchPoints.getPoint(0).event == 0x00) {
96 return _touchPoints;
97 }
98 // Swap XY or mirroring coordinates,if set
100 }
101
102 return _touchPoints;
103}
104
106{
107 switch (_chipID) {
108 case CST9220_CHIP_ID:
109 return "CST9220";
110 case CST9217_CHIP_ID:
111 return "CST9217";
112 default:
113 break;
114 }
115 return "UNKNOWN";
116}
117
119{
120
121 uint8_t buffer[2] = {0};
122 // Enter command mode
123 setMode(MODE_DEBUG_INFO);
124 //Send sleep command
126 writeBuff(buffer, 2);
127#ifdef ARDUINO_ARCH_ESP32
128 if (_pinsCfg.irqPin != -1) {
129 hal->pinMode(_pinsCfg.irqPin, OPEN_DRAIN);
130 }
131 if (_pinsCfg.rstPin != -1) {
132 hal->pinMode(_pinsCfg.rstPin, OPEN_DRAIN);
133 }
134#endif
135}
136
138{
139 _HButtonCallback = cb;
140 _userData = user_data;
141}
142
146uint32_t TouchDrvCST92xx::readWordFromMem(uint8_t type, uint16_t mem_addr)
147{
148 int res = 0;
149 uint8_t write_buffer[4] = {0};
150 uint8_t read_buffer[4] = {0};
151
154 }
155
156
157 write_buffer[0] = 0xA0;
158 write_buffer[1] = 0x10;
159 write_buffer[2] = type;
160
161 res = writeBuff(write_buffer, 3);
162 if (res != 0) {
163 SENSORLIB_LOG_E("Write 0A010 failed");
164 goto ERROR;
165 }
166
167 write_buffer[0] = 0xA0;
168 write_buffer[1] = 0x0C;
169 write_buffer[2] = mem_addr;
170 write_buffer[3] = mem_addr >> 8;
171
172 res = writeBuff(write_buffer, 4);
173 if (res != 0) {
174 SENSORLIB_LOG_E("Write 0A00C failed");
175 goto ERROR;
176 }
177
178 write_buffer[0] = 0xA0;
179 write_buffer[1] = 0x04;
180 write_buffer[2] = 0xE4;
181
182 res = writeBuff(write_buffer, 3);
183 if (res != 0) {
184 SENSORLIB_LOG_E("Write 0A004E4 failed");
185 goto ERROR;
186 }
187
188 for (uint8_t t = 0;; t++) {
189 if (t >= 100) {
190 goto ERROR;
191 }
192 write_buffer[0] = 0xA0;
193 write_buffer[1] = 0x04;
194 res = writeThenRead(write_buffer, 2, read_buffer, 1);
195 if (res != 0) {
196 SENSORLIB_LOG_E("Write 0A004 failed");
197 goto ERROR;
198 }
199
200 if (read_buffer[0] == 0x00) {
201 break;
202 }
203 }
204 write_buffer[0] = 0xA0;
205 write_buffer[1] = 0x18;
206 res = writeThenRead(write_buffer, 2, read_buffer, 4);
207 if (res != 0) {
208 goto ERROR;
209 }
210
213 }
214
215 return ((uint32_t)(read_buffer[0])) |
216 (((uint32_t)(read_buffer[1])) << 8) |
217 (((uint32_t)(read_buffer[2])) << 16) |
218 (((uint32_t)(read_buffer[3])) << 24);
219ERROR:
222 }
223 return 0;
224}
225
226uint32_t TouchDrvCST92xx::getChipType()
227{
228 return chipType;
229}
230
234bool TouchDrvCST92xx::enterBootloader(void)
235{
236 int16_t res = 0;
237 uint8_t check_cnt = 0;
238 uint8_t write_buffer[4] = {0};
239 uint8_t read_buffer[4] = {0};
240
243 }
244
245 for (uint8_t i = 10;; i += 2) {
246
247 if (i > 20) {
248 SENSORLIB_LOG_E("Enter boot:try timeout");
249 goto ERROR;
250 }
251
252 reset();
253
254 hal->delay(i);
255
256 for (check_cnt = 0; check_cnt < 5; check_cnt++) {
257 write_buffer[0] = 0xA0;
258 write_buffer[1] = 0x01;
259 write_buffer[2] = 0xAA;
260 res = writeBuff(write_buffer, 3);
261 if (res != 0) {
262 hal->delay(2);
263 continue;
264 }
265 hal->delay(2);
266 write_buffer[0] = 0xA0;
267 write_buffer[1] = 0x02;
268 res = writeThenRead(write_buffer, 2, read_buffer, 2);
269 if (res != 0) {
270 hal->delay(2);
271 continue;
272 }
273 if ((read_buffer[0] == 0x55) && (read_buffer[1] == 0xB0)) {
274 break;
275 }
276 }
277 if ((read_buffer[0] == 0x55) && (read_buffer[1] == 0xB0)) {
278 break;
279 }
280 }
281
282 write_buffer[0] = 0xA0;
283 write_buffer[1] = 0x01;
284 write_buffer[2] = 0x00;
285 res = writeBuff(write_buffer, 3);
286 if (res != 0) {
287 SENSORLIB_LOG_E("Enter boot exit error");
288 goto ERROR;
289 }
292 }
293 SENSORLIB_LOG_D("Enter boot mode success!");
294 return true;
295
296ERROR:
299 }
300 return false;
301
302}
303
304bool TouchDrvCST92xx::getAttribute()
305{
306 reset();
307
308 // Wait exit boot mode
309 hal->delay(30);
310
311 uint8_t buffer[8];
312 // Enter Command mode
313 writeReg(0xD1, 0x01);
314 hal->delay(10);
315 uint8_t write_buffer[2] = {0xD1, 0xFC};
316 writeThenRead(write_buffer, 2, buffer, 4);
317 uint32_t checkcode = 0;
318 checkcode = buffer[3];
319 checkcode <<= 8;
320 checkcode |= buffer[2];
321 checkcode <<= 8;
322 checkcode |= buffer[1];
323 checkcode <<= 8;
324 checkcode |= buffer[0];
325
326 SENSORLIB_LOG_I("Chip checkcode:0x%" PRIx32 ".", checkcode);
327
328 write_buffer[0] = 0xD1;
329 write_buffer[1] = 0xF8;
330 writeThenRead(write_buffer, 2, buffer, 4);
331 _touchConfig.resolutionX = ( buffer[1] << 8) | buffer[0];
332 _touchConfig.resolutionY = ( buffer[3] << 8) | buffer[2];
333 SENSORLIB_LOG_I("Chip resolution X:%u Y:%u", _touchConfig.resolutionX, _touchConfig.resolutionY);
334
335 write_buffer[0] = 0xD2;
336 write_buffer[1] = 0x04;
337 writeThenRead(write_buffer, 2, buffer, 4);
338 chipType = buffer[3];
339 chipType <<= 8;
340 chipType |= buffer[2];
341
342
343 uint32_t ProjectID = buffer[1];
344 ProjectID <<= 8;
345 ProjectID |= buffer[0];
346 SENSORLIB_LOG_I("Chip type :0x%x, ProjectID:0x%" PRIx32, chipType, ProjectID);
347
348 write_buffer[0] = 0xD2;
349 write_buffer[1] = 0x08;
350 writeThenRead(write_buffer, 2, buffer, 8);
351
352 uint32_t fwVersion = buffer[3];
353 fwVersion <<= 8;
354 fwVersion |= buffer[2];
355 fwVersion <<= 8;
356 fwVersion |= buffer[1];
357 fwVersion <<= 8;
358 fwVersion |= buffer[0];
359
360 uint32_t checksum = buffer[7];
361 checksum <<= 8;
362 checksum |= buffer[6];
363 checksum <<= 8;
364 checksum |= buffer[5];
365 checksum <<= 8;
366 checksum |= buffer[4];
367
368 SENSORLIB_LOG_I("Chip ic version:0x%" PRIx32 ", checksum:0x%" PRIx32,
369 fwVersion, checksum);
370
371 if (fwVersion == 0xA5A5A5A5) {
372 SENSORLIB_LOG_E("Chip ic don't have firmware.");
373 return false;
374 }
375 if ((checkcode & 0xffff0000) != 0xCACA0000) {
376 SENSORLIB_LOG_E("Firmware info read error.");
377 return false;
378 }
379
381 SENSORLIB_LOG_E("Chip type error 0x%x", chipType);
382 return false;
383 }
384
385 return true;
386}
387
388bool TouchDrvCST92xx::setMode(uint8_t mode)
389{
390 uint8_t read_buffer[4] = {0};
391 uint8_t write_buffer[4] = {0};
392 uint8_t i = 0;
393 int16_t res = -1;
394 uint8_t mode_cmd = 0;
395
396 for (i = 0; i < 3; i++) {
397 write_buffer[0] = 0xD1;
398 write_buffer[1] = 0x1E;
399 res = writeBuff(write_buffer, 2);
400 if (res != 0) {
401 hal->delay(200);
402 continue;
403 }
404 write_buffer[0] = 0xD1;
405 write_buffer[1] = 0x1E;
406 res = writeBuff(write_buffer, 2);
407 if (res != 0) {
408 hal->delay(200);
409 continue;
410 }
411 write_buffer[0] = 0x00;
412 write_buffer[1] = 0x02;
413 res = writeThenRead(write_buffer, 2, read_buffer, 4);
414 if (res != 0) {
415 hal->delay(200);
416 continue;
417 }
418 if (read_buffer[1] == 0x1E)
419 break;
420 }
421
422 switch (mode) {
423 case MODE_NORMAL: {
424 SENSORLIB_LOG_D("set_work_mode: ENUM_MODE_NORMAL");
425 write_buffer[0] = sensorlib::_highByte(REG_NORMAL_MODE);
426 write_buffer[1] = sensorlib::_lowByte(REG_NORMAL_MODE);
427 break;
428 }
429 case MODE_DEBUG_DIFF: {
430 SENSORLIB_LOG_D("set_work_mode: ENUM_MODE_DEBUG_DIFF");
431 write_buffer[0] = sensorlib::_highByte(REG_DIFF_MODE);
432 write_buffer[1] = sensorlib::_lowByte(REG_DIFF_MODE);
433 break;
434 }
435 case MODE_DEBUG_RAWDATA: {
436 SENSORLIB_LOG_D("set_work_mode: ENUM_MODE_DEBUG_RAWDATA");
437 write_buffer[0] = sensorlib::_highByte(REG_RAW_MODE);
438 write_buffer[1] = sensorlib::_lowByte(REG_RAW_MODE);
439 break;
440 }
441 case MODE_DEBUG_INFO: {
442 SENSORLIB_LOG_D("set_work_mode: ENUM_MODE_DEBUG_INFO");
443 write_buffer[0] = sensorlib::_highByte(REG_DEBUG_MODE);
444 write_buffer[1] = sensorlib::_lowByte(REG_DEBUG_MODE);
445 break;
446 }
447 case MODE_FACTORY: {
448 SENSORLIB_LOG_D("set_work_mode: ENUM_MODE_FACTORY");
449 for (i = 0; i < 10; i++) {
450 write_buffer[0] = sensorlib::_highByte(REG_FACTORY_MODE);
451 write_buffer[1] = sensorlib::_lowByte(REG_FACTORY_MODE);
452 res = writeBuff(write_buffer, 2);
453 if (res != 0) {
454 hal->delay(1);
455 continue;
456 }
457 hal->delay(10);
458 write_buffer[0] = 0x00;
459 write_buffer[1] = 0x09;
460 res = writeThenRead(write_buffer, 2, read_buffer, 1);
461 if (res != 0) {
462 hal->delay(1);
463 continue;
464 }
465 if (read_buffer[0] == 0x14)
466 break;
467 }
468 write_buffer[0] = 0xD1;
469 write_buffer[1] = 0x19;
470 res = writeBuff(write_buffer, 2);
471 if (res != 0) {
472 SENSORLIB_LOG_E("set_work_mode 0xD119 error");
473 return false;
474 }
475 break;
476 }
478 SENSORLIB_LOG_D("set_work_mode: ENUM_MODE_FACTORY_LOWDRV");
479 write_buffer[0] = 0xD1;
480 write_buffer[1] = 0x11;
481 break;
482 }
484 SENSORLIB_LOG_D("set_work_mode: ENUM_MODE_FACTORY_HIGHDRV");
485 write_buffer[0] = 0xD1;
486 write_buffer[1] = 0x10;
487 break;
488 }
489 case MODE_FACTORY_SHORT: {
490 SENSORLIB_LOG_D("set_work_mode: ENUM_MODE_FACTORY_SHORT");
491 write_buffer[0] = 0xD1;
492 write_buffer[1] = 0x12;
493 break;
494 }
495 case 0XFE: {
496 SENSORLIB_LOG_D("set_work_mode: 0xFE");
497 write_buffer[0] = 0xD1;
498 write_buffer[1] = 0x1F;
499 break;
500 }
501 default: {
502 SENSORLIB_LOG_D("set_work_mode: NA return");
503 return 0;
504 }
505 }
506 mode_cmd = write_buffer[1];
507 res = writeBuff(write_buffer, 2);
508 if (res != 0) {
509 SENSORLIB_LOG_E("set_work_mode 0x%x 0x%x error", write_buffer[0], write_buffer[1]);
510 return false;
511 }
512 write_buffer[0] = 0x00;
513 write_buffer[1] = 0x02;
514 res = writeThenRead(write_buffer, 2, read_buffer, 2);
515 if (res != 0) {
516 SENSORLIB_LOG_E("set_work_mode read 0x0002 failed : 0x%X 0x%X", read_buffer[0], read_buffer[1]);
517 }
518 if (mode_cmd != read_buffer[1]) {
519 SENSORLIB_LOG_E("set work mode read 0x0002=0x%x failed", read_buffer[1]);
520 return false;
521 }
522 hal->delay(10);
523 return true;
524}
525
526bool TouchDrvCST92xx::initImpl(uint8_t)
527{
528 if (!getAttribute()) {
529 return false;
530 }
531
533
534 SENSORLIB_LOG_D("Touch type:%s", getModelName());
535
537
538 return true;
539}
540
541#endif
@ ERROR
Error state.
#define SENSORLIB_LOG_I(...)
#define SENSORLIB_LOG_E(...)
#define SENSORLIB_LOG_D(...)
void addrToBeBuf(uint32_t addr, uint8_t buf[4])
@license MIT License
std::unique_ptr< SensorHal > hal
int writeBuff(uint8_t *buf, size_t len)
int writeReg(uint8_t reg, uint8_t val)
int writeThenRead(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len)
void setAddress(uint8_t address)
void sleep() override
Puts the touch driver to sleep.
static constexpr uint16_t CST9217_CHIP_ID
static constexpr uint16_t REG_FACTORY_MODE
static constexpr uint16_t REG_DEBUG_MODE
void setCoverScreenCallback(HomeButtonCallback cb, void *user_data=NULL)
Set the cover screen callback.
static constexpr uint8_t MAX_FINGER_NUM
static constexpr uint16_t REG_SLEEP_MODE
const char * getModelName() override
Get the model name.
static constexpr uint16_t REG_RAW_MODE
static constexpr uint16_t REG_DIFF_MODE
static constexpr uint8_t CST92XX_BOOT_ADDRESS
const TouchPoints & getTouchPoints() override
Get the touch points.
static constexpr uint16_t REG_NORMAL_MODE
static constexpr uint16_t CST9220_CHIP_ID
static constexpr uint8_t CST92XX_ACK
static constexpr uint16_t REG_READ
TouchDrvCST92xx()
Constructor for the touch driver.
uint32_t _chipID
Chip identification value.
TouchConfig _touchConfig
Configuration for coordinate transformations.
void(*)(void *user_data) HomeButtonCallback
Callback type for home button events.
virtual void reset()
Reset the touch controller.
void * _userData
User data for home button callback.
virtual void updateXY(TouchPoints &points) const
Apply coordinate transformations (swap, scale, mirror) to all touch points.
TouchPinsCfg _pinsCfg
Configuration for reset and interrupt pins.
TouchPoints _touchPoints
Touch points data.
uint8_t _maxTouchPoints
Maximum supported touch points.
HomeButtonCallback _HButtonCallback
Home button callback function.
Container for touch point data and optional gesture information.
bool addPoint(uint16_t x, uint16_t y, uint16_t pressure=0, uint8_t id=0, uint8_t event=0)
Adds a touch point to the container.
void clear()
Clears all stored touch points and gesture.
TouchPoint & getPoint(uint8_t index)
Gets a reference to a touch point at the specified index.
constexpr uint8_t _lowByte(uint16_t w)
Definition SensorLib.h:69
constexpr uint8_t _highByte(uint16_t w)
Definition SensorLib.h:73
uint8_t i
uint8_t event
Event type based on actual hardware feedback, most touch devices do not support.
int16_t x[5]
int16_t y[5]