53 ESP_LOGV(TAG,
"Continuing CST328 setup...");
58 I2C_FAIL_ON_ERROR(this->
write_register16(CST_WM_DEBUG_INFO, buf, 0), TAG,
"Failed to enter debug/info mode");
59 I2C_FAIL_ON_ERROR(this->
read_register16(CST_REG_FW_CRC_AND_BOOT_TIME, buf, 4), TAG,
60 "Failed to read FW CRC and boot time");
62 uint16_t fw_crc = buf[2] + (buf[3] << 8);
63 if (fw_crc != CST328_FW_CRC) {
64 ESP_LOGE(TAG,
"Error: Firmware CRC mismatch, expected 0x%04X but got 0x%04X", CST328_FW_CRC, fw_crc);
69 I2C_FAIL_ON_ERROR(this->
read_register16(CST_REG_CHIP_TYPE_AND_PROJECT_ID, buf, 4), TAG,
70 "Failed to read chip and project ID");
72 this->
chip_id_ = buf[2] + (buf[3] << 8);
74 ESP_LOGD(TAG,
"Chip ID %X, project ID %X", this->
chip_id_, this->project_id_);
75 I2C_FAIL_ON_ERROR(this->
read_register16(CST_REG_FW_REVISION, buf, 4), TAG,
"Failed to read FW version");
90 I2C_WARN_ON_ERROR(this->
write_register16(CST_WM_NORMAL, buf, 0), TAG,
"Failed to enter normal mode");
91 I2C_WARN_ON_ERROR(this->
read_register16(CST_REG_TOUCH_INFORMATION, &data_byte, 1), TAG,
"Failed to read sync");
92 I2C_WARN_ON_ERROR(this->
write_register16(CST_REG_TOUCH_INFORMATION, &CST328_SYNC_BYTE, 1), TAG,
93 "Failed to write sync");
101 ESP_LOGV(TAG,
"CST328 setup complete");
130 uint8_t touch_data[CST328_TOUCH_DATA_SIZE];
135 ESP_LOGW(TAG,
"Failed to read touch data");
141 uint8_t touch_cnt = touch_data[CST_REG_FINGER_COUNT_IDX] & 0x0F;
142 if (touch_cnt == 0 || touch_cnt > CST328_TOUCH_MAX_POINTS) {
147 uint8_t data_idx = 0;
148 for (uint8_t i = 0; i < touch_cnt; i++) {
149 uint8_t
id = touch_data[data_idx] >> 4;
150 int16_t
x = (touch_data[data_idx + 1] << 4) | ((touch_data[data_idx + 3] >> 4) & 0x0F);
151 int16_t
y = (touch_data[data_idx + 2] << 4) | (touch_data[data_idx + 3] & 0x0F);
152 int16_t
z = touch_data[data_idx + 4];
155 data_idx += (i == 0) ? 7 : 5;
159 bool cleanup_error =
false;
164 ESP_LOGW(TAG,
"Failed to clean up touch registers");