Skip to content

Commit

Permalink
Merge pull request #226 from espressif2022/feature/lcd_touch_gt911_lo…
Browse files Browse the repository at this point in the history
…w_power

lcd_touch_gt911: Add low_power interface and button. (BSP-380)
  • Loading branch information
espzav authored Sep 25, 2023
2 parents fff261c + 27948a9 commit e22d008
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 9 deletions.
114 changes: 109 additions & 5 deletions components/lcd_touch/esp_lcd_touch_gt911/esp_lcd_touch_gt911.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -20,15 +20,23 @@
static const char *TAG = "GT911";

/* GT911 registers */
#define ESP_LCD_TOUCH_GT911_READ_XY_REG (0x814E)
#define ESP_LCD_TOUCH_GT911_CONFIG_REG (0x8047)
#define ESP_LCD_TOUCH_GT911_PRODUCT_ID_REG (0x8140)
#define ESP_LCD_TOUCH_GT911_READ_KEY_REG (0x8093)
#define ESP_LCD_TOUCH_GT911_READ_XY_REG (0x814E)
#define ESP_LCD_TOUCH_GT911_CONFIG_REG (0x8047)
#define ESP_LCD_TOUCH_GT911_PRODUCT_ID_REG (0x8140)
#define ESP_LCD_TOUCH_GT911_ENTER_SLEEP (0x8040)

/* GT911 support key num */
#define ESP_GT911_TOUCH_MAX_BUTTONS (4)

/*******************************************************************************
* Function definitions
*******************************************************************************/
static esp_err_t esp_lcd_touch_gt911_read_data(esp_lcd_touch_handle_t tp);
static bool esp_lcd_touch_gt911_get_xy(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num);
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
static esp_err_t esp_lcd_touch_gt911_get_button_state(esp_lcd_touch_handle_t tp, uint8_t n, uint8_t *state);
#endif
static esp_err_t esp_lcd_touch_gt911_del(esp_lcd_touch_handle_t tp);

/* I2C read/write */
Expand All @@ -40,6 +48,10 @@ static esp_err_t touch_gt911_reset(esp_lcd_touch_handle_t tp);
/* Read status and config register */
static esp_err_t touch_gt911_read_cfg(esp_lcd_touch_handle_t tp);

/* GT911 enter/exit sleep mode */
static esp_err_t esp_lcd_touch_gt911_enter_sleep(esp_lcd_touch_handle_t tp);
static esp_err_t esp_lcd_touch_gt911_exit_sleep(esp_lcd_touch_handle_t tp);

/*******************************************************************************
* Public API functions
*******************************************************************************/
Expand All @@ -62,7 +74,12 @@ esp_err_t esp_lcd_touch_new_i2c_gt911(const esp_lcd_panel_io_handle_t io, const
/* Only supported callbacks are set */
esp_lcd_touch_gt911->read_data = esp_lcd_touch_gt911_read_data;
esp_lcd_touch_gt911->get_xy = esp_lcd_touch_gt911_get_xy;
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
esp_lcd_touch_gt911->get_button_state = esp_lcd_touch_gt911_get_button_state;
#endif
esp_lcd_touch_gt911->del = esp_lcd_touch_gt911_del;
esp_lcd_touch_gt911->enter_sleep = esp_lcd_touch_gt911_enter_sleep;
esp_lcd_touch_gt911->exit_sleep = esp_lcd_touch_gt911_exit_sleep;

/* Mutex */
esp_lcd_touch_gt911->data.lock.owner = portMUX_FREE_VAL;
Expand Down Expand Up @@ -117,6 +134,41 @@ esp_err_t esp_lcd_touch_new_i2c_gt911(const esp_lcd_panel_io_handle_t io, const
return ret;
}

static esp_err_t esp_lcd_touch_gt911_enter_sleep(esp_lcd_touch_handle_t tp)
{
esp_err_t err = touch_gt911_i2c_write(tp, ESP_LCD_TOUCH_GT911_ENTER_SLEEP, 0x05);
ESP_RETURN_ON_ERROR(err, TAG, "Enter Sleep failed!");

return ESP_OK;
}

static esp_err_t esp_lcd_touch_gt911_exit_sleep(esp_lcd_touch_handle_t tp)
{
esp_err_t ret;
esp_lcd_touch_handle_t esp_lcd_touch_gt911 = tp;

if (esp_lcd_touch_gt911->config.int_gpio_num != GPIO_NUM_NC) {
const gpio_config_t int_gpio_config_high = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = BIT64(esp_lcd_touch_gt911->config.int_gpio_num)
};
ret = gpio_config(&int_gpio_config_high);
ESP_RETURN_ON_ERROR(ret, TAG, "High GPIO config failed");
gpio_set_level(esp_lcd_touch_gt911->config.int_gpio_num, 1);

vTaskDelay(pdMS_TO_TICKS(5));

const gpio_config_t int_gpio_config_float = {
.mode = GPIO_MODE_OUTPUT_OD,
.pin_bit_mask = BIT64(esp_lcd_touch_gt911->config.int_gpio_num)
};
ret = gpio_config(&int_gpio_config_float);
ESP_RETURN_ON_ERROR(ret, TAG, "Float GPIO config failed");
}

return ESP_OK;
}

static esp_err_t esp_lcd_touch_gt911_read_data(esp_lcd_touch_handle_t tp)
{
esp_err_t err;
Expand All @@ -133,7 +185,36 @@ static esp_err_t esp_lcd_touch_gt911_read_data(esp_lcd_touch_handle_t tp)
/* Any touch data? */
if ((buf[0] & 0x80) == 0x00) {
touch_gt911_i2c_write(tp, ESP_LCD_TOUCH_GT911_READ_XY_REG, clear);
} else {
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
} else if ((buf[0] & 0x10) == 0x10) {
/* Read all keys */
uint8_t key_max = ((ESP_GT911_TOUCH_MAX_BUTTONS < CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS) ? \
(ESP_GT911_TOUCH_MAX_BUTTONS) : (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS));
err = touch_gt911_i2c_read(tp, ESP_LCD_TOUCH_GT911_READ_KEY_REG, &buf[0], key_max);
ESP_RETURN_ON_ERROR(err, TAG, "I2C read error!");

/* Clear all */
touch_gt911_i2c_write(tp, ESP_LCD_TOUCH_GT911_READ_XY_REG, clear);
ESP_RETURN_ON_ERROR(err, TAG, "I2C write error!");

portENTER_CRITICAL(&tp->data.lock);

/* Buttons count */
tp->data.buttons = key_max;
for (i = 0; i < key_max; i++) {
tp->data.button[i].status = buf[0] ? 1 : 0;
}

portEXIT_CRITICAL(&tp->data.lock);
#endif
} else if ((buf[0] & 0x80) == 0x80) {
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
portENTER_CRITICAL(&tp->data.lock);
for (i = 0; i < CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS; i++) {
tp->data.button[i].status = 0;
}
portEXIT_CRITICAL(&tp->data.lock);
#endif
/* Count of touched points */
touch_cnt = buf[0] & 0x0f;
if (touch_cnt > 5 || touch_cnt == 0) {
Expand Down Expand Up @@ -198,6 +279,29 @@ static bool esp_lcd_touch_gt911_get_xy(esp_lcd_touch_handle_t tp, uint16_t *x, u
return (*point_num > 0);
}

#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0)
static esp_err_t esp_lcd_touch_gt911_get_button_state(esp_lcd_touch_handle_t tp, uint8_t n, uint8_t *state)
{
esp_err_t err = ESP_OK;
assert(tp != NULL);
assert(state != NULL);

*state = 0;

portENTER_CRITICAL(&tp->data.lock);

if (n > tp->data.buttons) {
err = ESP_ERR_INVALID_ARG;
} else {
*state = tp->data.button[n].status;
}

portEXIT_CRITICAL(&tp->data.lock);

return err;
}
#endif

static esp_err_t esp_lcd_touch_gt911_del(esp_lcd_touch_handle_t tp)
{
assert(tp != NULL);
Expand Down
4 changes: 2 additions & 2 deletions components/lcd_touch/esp_lcd_touch_gt911/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: "1.0.7~1"
version: "1.1.0"
description: ESP LCD Touch GT911 - touch controller GT911
url: https://github.com/espressif/esp-bsp/tree/master/components/lcd_touch/esp_lcd_touch_gt911
dependencies:
idf: ">=4.4.2"
esp_lcd_touch:
version: "^1.0.4"
version: "^1.1.0"
public: true
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -34,8 +34,12 @@ esp_err_t esp_lcd_touch_new_i2c_gt911(const esp_lcd_panel_io_handle_t io, const
/**
* @brief I2C address of the GT911 controller
*
* @note When power-on detects low level of the interrupt gpio, address is 0x5D.
* @note Interrupt gpio is high level, address is 0x14.
*
*/
#define ESP_LCD_TOUCH_IO_I2C_GT911_ADDRESS (0x5D)
#define ESP_LCD_TOUCH_IO_I2C_GT911_ADDRESS (0x5D)
#define ESP_LCD_TOUCH_IO_I2C_GT911_ADDRESS_BACKUP (0x14)

/**
* @brief Touch IO configuration structure
Expand Down

0 comments on commit e22d008

Please sign in to comment.