atanisoft / esp_lcd_touch_xpt2046

MIT License
14 stars 4 forks source link

interrupt feature - incorrect gpio config implementation #8

Closed ttt470 closed 1 month ago

ttt470 commented 1 month ago

ISSUE

I couldn't manage to make the interrupt feature to work. For example, the following config does not work at all:

     const esp_lcd_touch_config_t tpCfg = {
        .x_max = 240,
        .y_max = 320,
        .rst_gpio_num = -1,
        .int_gpio_num = GPIO_NUM_40,
        .levels = {
            .reset = 0,
            .interrupt = 0,
        },
        .flags = {
            .swap_xy = false,
            .mirror_x = false,
            .mirror_y = false,
        },
    };

The issue is in the implementation of esp_lcd_touch_new_spi_xpt2046 from lines 100-104:

        gpio_config_t cfg;
        memset(&cfg, 0, sizeof(gpio_config_t));
        esp_rom_gpio_pad_select_gpio(config->int_gpio_num);
        cfg.pin_bit_mask = BIT64(config->int_gpio_num);
        cfg.mode = GPIO_MODE_INPUT;

Precisely, the gpio_config_t::intr_type is set to 0 (ie GPIO_INTR_DISABLE) from the memset call (see line 101).

FIX

I was able to use the interrupt feature by replacing the lines 100-104 as follows:

FROM :

        gpio_config_t cfg;
        memset(&cfg, 0, sizeof(gpio_config_t));
        esp_rom_gpio_pad_select_gpio(config->int_gpio_num);
        cfg.pin_bit_mask = BIT64(config->int_gpio_num);
        cfg.mode = GPIO_MODE_INPUT;

TO :

        esp_rom_gpio_pad_select_gpio(config->int_gpio_num);
        gpio_config_t cfg = {
            .mode = GPIO_MODE_INPUT,
            .intr_type = (config->levels.interrupt ? GPIO_INTR_POSEDGE : GPIO_INTR_NEGEDGE),
            .pin_bit_mask = BIT64(config->int_gpio_num)
        };

PS: it would be nice to document the special treatment of esp_lcd_touch_config_t::interrupt_callback from line 110.

NB: The fix was adapted from the esp tt21100 driver.

tested version: 2d31c51

atanisoft commented 1 month ago

If you can, please raise a PR to ONLY add the initialization of intr_type and not convert to struct initializer as you show in your proposal.