[gt911] i2c fixes (#9822)

This commit is contained in:
Clyde Stubbs 2025-07-24 07:52:07 +10:00 committed by Jesse Hills
parent c9793f3741
commit 93028a4d90
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A

View File

@ -8,6 +8,8 @@ namespace gt911 {
static const char *const TAG = "gt911.touchscreen"; static const char *const TAG = "gt911.touchscreen";
static const uint8_t PRIMARY_ADDRESS = 0x5D; // default I2C address for GT911
static const uint8_t SECONDARY_ADDRESS = 0x14; // secondary I2C address for GT911
static const uint8_t GET_TOUCH_STATE[2] = {0x81, 0x4E}; static const uint8_t GET_TOUCH_STATE[2] = {0x81, 0x4E};
static const uint8_t CLEAR_TOUCH_STATE[3] = {0x81, 0x4E, 0x00}; static const uint8_t CLEAR_TOUCH_STATE[3] = {0x81, 0x4E, 0x00};
static const uint8_t GET_TOUCHES[2] = {0x81, 0x4F}; static const uint8_t GET_TOUCHES[2] = {0x81, 0x4F};
@ -18,8 +20,7 @@ static const size_t MAX_BUTTONS = 4; // max number of buttons scanned
#define ERROR_CHECK(err) \ #define ERROR_CHECK(err) \
if ((err) != i2c::ERROR_OK) { \ if ((err) != i2c::ERROR_OK) { \
ESP_LOGE(TAG, "Failed to communicate!"); \ this->status_set_warning("Communication failure"); \
this->status_set_warning(); \
return; \ return; \
} }
@ -30,31 +31,31 @@ void GT911Touchscreen::setup() {
this->reset_pin_->setup(); this->reset_pin_->setup();
this->reset_pin_->digital_write(false); this->reset_pin_->digital_write(false);
if (this->interrupt_pin_ != nullptr) { if (this->interrupt_pin_ != nullptr) {
// The interrupt pin is used as an input during reset to select the I2C address. // temporarily set the interrupt pin to output to control address selection
this->interrupt_pin_->pin_mode(gpio::FLAG_OUTPUT); this->interrupt_pin_->pin_mode(gpio::FLAG_OUTPUT);
this->interrupt_pin_->setup();
this->interrupt_pin_->digital_write(false); this->interrupt_pin_->digital_write(false);
} }
delay(2); delay(2);
this->reset_pin_->digital_write(true); this->reset_pin_->digital_write(true);
delay(50); // NOLINT delay(50); // NOLINT
if (this->interrupt_pin_ != nullptr) { }
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT); if (this->interrupt_pin_ != nullptr) {
this->interrupt_pin_->setup(); // set pre-configured input mode
} this->interrupt_pin_->setup();
} }
// check the configuration of the int line. // check the configuration of the int line.
uint8_t data[4]; uint8_t data[4];
err = this->write(GET_SWITCHES, 2); err = this->write(GET_SWITCHES, sizeof(GET_SWITCHES));
if (err != i2c::ERROR_OK && this->address_ == PRIMARY_ADDRESS) {
this->address_ = SECONDARY_ADDRESS;
err = this->write(GET_SWITCHES, sizeof(GET_SWITCHES));
}
if (err == i2c::ERROR_OK) { if (err == i2c::ERROR_OK) {
err = this->read(data, 1); err = this->read(data, 1);
if (err == i2c::ERROR_OK) { if (err == i2c::ERROR_OK) {
ESP_LOGD(TAG, "Read from switches: 0x%02X", data[0]); ESP_LOGD(TAG, "Read from switches at address 0x%02X: 0x%02X", this->address_, data[0]);
if (this->interrupt_pin_ != nullptr) { if (this->interrupt_pin_ != nullptr) {
// datasheet says NOT to use pullup/down on the int line.
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT);
this->interrupt_pin_->setup();
this->attach_interrupt_(this->interrupt_pin_, this->attach_interrupt_(this->interrupt_pin_,
(data[0] & 1) ? gpio::INTERRUPT_FALLING_EDGE : gpio::INTERRUPT_RISING_EDGE); (data[0] & 1) ? gpio::INTERRUPT_FALLING_EDGE : gpio::INTERRUPT_RISING_EDGE);
} }
@ -63,7 +64,7 @@ void GT911Touchscreen::setup() {
if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) { if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
// no calibration? Attempt to read the max values from the touchscreen. // no calibration? Attempt to read the max values from the touchscreen.
if (err == i2c::ERROR_OK) { if (err == i2c::ERROR_OK) {
err = this->write(GET_MAX_VALUES, 2); err = this->write(GET_MAX_VALUES, sizeof(GET_MAX_VALUES));
if (err == i2c::ERROR_OK) { if (err == i2c::ERROR_OK) {
err = this->read(data, sizeof(data)); err = this->read(data, sizeof(data));
if (err == i2c::ERROR_OK) { if (err == i2c::ERROR_OK) {
@ -75,15 +76,12 @@ void GT911Touchscreen::setup() {
} }
} }
if (err != i2c::ERROR_OK) { if (err != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Failed to read calibration values from touchscreen!"); this->mark_failed("Failed to read calibration");
this->mark_failed();
return; return;
} }
} }
if (err != i2c::ERROR_OK) { if (err != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Failed to communicate!"); this->mark_failed("Failed to communicate");
this->mark_failed();
return;
} }
ESP_LOGCONFIG(TAG, "GT911 Touchscreen setup complete"); ESP_LOGCONFIG(TAG, "GT911 Touchscreen setup complete");
@ -94,7 +92,7 @@ void GT911Touchscreen::update_touches() {
uint8_t touch_state = 0; uint8_t touch_state = 0;
uint8_t data[MAX_TOUCHES + 1][8]; // 8 bytes each for each point, plus extra space for the key byte uint8_t data[MAX_TOUCHES + 1][8]; // 8 bytes each for each point, plus extra space for the key byte
err = this->write(GET_TOUCH_STATE, sizeof(GET_TOUCH_STATE), false); err = this->write(GET_TOUCH_STATE, sizeof(GET_TOUCH_STATE));
ERROR_CHECK(err); ERROR_CHECK(err);
err = this->read(&touch_state, 1); err = this->read(&touch_state, 1);
ERROR_CHECK(err); ERROR_CHECK(err);
@ -106,7 +104,7 @@ void GT911Touchscreen::update_touches() {
return; return;
} }
err = this->write(GET_TOUCHES, sizeof(GET_TOUCHES), false); err = this->write(GET_TOUCHES, sizeof(GET_TOUCHES));
ERROR_CHECK(err); ERROR_CHECK(err);
// num_of_touches is guaranteed to be 0..5. Also read the key data // num_of_touches is guaranteed to be 0..5. Also read the key data
err = this->read(data[0], sizeof(data[0]) * num_of_touches + 1); err = this->read(data[0], sizeof(data[0]) * num_of_touches + 1);
@ -132,6 +130,7 @@ void GT911Touchscreen::dump_config() {
ESP_LOGCONFIG(TAG, "GT911 Touchscreen:"); ESP_LOGCONFIG(TAG, "GT911 Touchscreen:");
LOG_I2C_DEVICE(this); LOG_I2C_DEVICE(this);
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_); LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
} }
} // namespace gt911 } // namespace gt911