From cbae0186ca69ccff988bca3e3edea8088eb94c52 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 15 Jul 2020 23:07:37 +0200 Subject: [PATCH 1/5] drivers/stmpe811: fix interrupt polarity & type The Interrupt on the stmpe811 is generated on the falling edge. By observing the rising edge, we will only get an event if the interrupt gets cleared. Also configure the interrupt to be edge triggered instead of level triggered. --- drivers/stmpe811/stmpe811.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/stmpe811/stmpe811.c b/drivers/stmpe811/stmpe811.c index a04a2b0810..780fd16ed5 100644 --- a/drivers/stmpe811/stmpe811.c +++ b/drivers/stmpe811/stmpe811.c @@ -162,7 +162,7 @@ int stmpe811_init(stmpe811_t *dev, const stmpe811_params_t * params, touch_event if ((dev->params.int_pin != GPIO_UNDEF) && cb) { DEBUG("[stmpe811] init: configuring touchscreen interrupt\n"); - gpio_init_int(dev->params.int_pin, GPIO_IN, GPIO_RISING, cb, arg); + gpio_init_int(dev->params.int_pin, GPIO_IN, GPIO_FALLING, cb, arg); /* Enable touchscreen interrupt */ ret += i2c_write_reg(STMPE811_DEV_I2C, STMPE811_DEV_ADDR, @@ -170,7 +170,7 @@ int stmpe811_init(stmpe811_t *dev, const stmpe811_params_t * params, touch_event /* Enable global interrupt */ ret += i2c_write_reg(STMPE811_DEV_I2C, STMPE811_DEV_ADDR, - STMPE811_INT_CTRL, STMPE811_INT_CTRL_GLOBAL_INT, 0); + STMPE811_INT_CTRL, STMPE811_INT_CTRL_GLOBAL_INT | STMPE811_INT_CTRL_INT_TYPE, 0); } if (ret < 0) { From f857b7116be0c74762c578a847e6275c2bb7d27e Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 15 Jul 2020 23:09:25 +0200 Subject: [PATCH 2/5] tests/driver_stmpe811: don't poll for touch events Now that the interrupt of the driver works properly, we don't have to rely on polling. Instead, wake the thread by touch interrupt. --- tests/driver_stmpe811/Makefile | 1 - tests/driver_stmpe811/main.c | 18 ++++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/driver_stmpe811/Makefile b/tests/driver_stmpe811/Makefile index ae5eeb40eb..5261b86335 100644 --- a/tests/driver_stmpe811/Makefile +++ b/tests/driver_stmpe811/Makefile @@ -2,7 +2,6 @@ BOARD ?= stm32f429i-disc1 include ../Makefile.tests_common -USEMODULE += xtimer USEMODULE += stmpe811 include $(RIOTBASE)/Makefile.include diff --git a/tests/driver_stmpe811/main.c b/tests/driver_stmpe811/main.c index e29361558a..a99740f985 100644 --- a/tests/driver_stmpe811/main.c +++ b/tests/driver_stmpe811/main.c @@ -20,25 +20,25 @@ #include -#include "xtimer.h" +#include "mutex.h" #include "stmpe811.h" #include "stmpe811_params.h" static void _touch_event_cb(void *arg) { - (void)arg; - puts("Pressed!"); + mutex_unlock(arg); } int main(void) { + mutex_t lock = MUTEX_INIT_LOCKED; stmpe811_t dev; puts("STMPE811 test application\n"); printf("+------------Initializing------------+\n"); - int ret = stmpe811_init(&dev, &stmpe811_params[0], _touch_event_cb, NULL); + int ret = stmpe811_init(&dev, &stmpe811_params[0], _touch_event_cb, &lock); if (ret != STMPE811_OK) { puts("[Error] Initialization failed"); return 1; @@ -51,8 +51,16 @@ int main(void) stmpe811_touch_state_t last_touch_state = current_touch_state; while (1) { + + /* wait for touch event */ + mutex_lock(&lock); + stmpe811_read_touch_state(&dev, ¤t_touch_state); + if (current_touch_state != last_touch_state) { + if (current_touch_state == STMPE811_TOUCH_STATE_PRESSED) { + puts("Pressed!"); + } if (current_touch_state == STMPE811_TOUCH_STATE_RELEASED) { puts("Released!"); } @@ -65,8 +73,6 @@ int main(void) stmpe811_read_touch_position(&dev, &position); printf("X: %i, Y:%i\n", position.x, position.y); } - - xtimer_usleep(10 * US_PER_MS); } return 0; From caead7bb75e986d318a5ed5a15fb526c3fbc7077 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 16 Jul 2020 10:25:30 +0200 Subject: [PATCH 3/5] drivers/stmpe811: always clear interrupt state --- drivers/stmpe811/stmpe811.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/stmpe811/stmpe811.c b/drivers/stmpe811/stmpe811.c index 780fd16ed5..70962c3726 100644 --- a/drivers/stmpe811/stmpe811.c +++ b/drivers/stmpe811/stmpe811.c @@ -262,8 +262,9 @@ int stmpe811_read_touch_state(const stmpe811_t *dev, stmpe811_touch_state_t *sta return -STMPE811_ERR_I2C; } + _clear_interrupt_status(dev); + if ((val & STMPE811_TSC_CTRL_STA)) { - _clear_interrupt_status(dev); *state = STMPE811_TOUCH_STATE_PRESSED; } else { From 37b096562aed8c00a55b817c2e1bcbb9a4832d07 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Tue, 21 Jul 2020 17:00:39 +0200 Subject: [PATCH 4/5] drivers/stmpe811: ensure fifo is not empty before reading touch position --- drivers/stmpe811/stmpe811.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/stmpe811/stmpe811.c b/drivers/stmpe811/stmpe811.c index 70962c3726..14c639da7e 100644 --- a/drivers/stmpe811/stmpe811.c +++ b/drivers/stmpe811/stmpe811.c @@ -194,6 +194,13 @@ int stmpe811_read_touch_position(stmpe811_t *dev, stmpe811_touch_position_t *pos /* Acquire I2C device */ i2c_acquire(STMPE811_DEV_I2C); + /* Ensure there's a least one position measured in the FIFO */ + uint8_t fifo_size = 0; + do { + i2c_read_reg(STMPE811_DEV_I2C, STMPE811_DEV_ADDR, + STMPE811_FIFO_SIZE, &fifo_size, 0); + } while (!fifo_size); + uint8_t xyz[4]; uint32_t xyz_ul; @@ -203,7 +210,6 @@ int stmpe811_read_touch_position(stmpe811_t *dev, stmpe811_touch_position_t *pos i2c_release(STMPE811_DEV_I2C); return -STMPE811_ERR_I2C; } - _reset_fifo(dev); /* Release I2C device */ i2c_release(STMPE811_DEV_I2C); From 9b3e94bf342ab307a6c988200839af3e63bc6f96 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Tue, 21 Jul 2020 21:25:35 +0200 Subject: [PATCH 5/5] tests/driver_stmpe811: update application README --- tests/driver_stmpe811/README.md | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/tests/driver_stmpe811/README.md b/tests/driver_stmpe811/README.md index 96d2bfbf77..0144f9ca5d 100644 --- a/tests/driver_stmpe811/README.md +++ b/tests/driver_stmpe811/README.md @@ -13,21 +13,17 @@ make -C tests/driver_stmpe811 flash term ## Expected output The application initializes the STMPE811 and displays "Pressed!" when a touch -event is detected. -Current touch positions are printed in terminal while holding the screen -pressed. +event is detected. The position of the touch event is also displayed. "Released" is displayed when the screen is released. ``` -2020-02-10 10:20:17,647 # Pressed! -2020-02-10 10:20:17,653 # X: 167, Y:81 -2020-02-10 10:20:17,659 # X: 165, Y:75 -2020-02-10 10:20:17,671 # X: 165, Y:69 -2020-02-10 10:20:17,689 # X: 166, Y:68 -2020-02-10 10:20:17,701 # X: 167, Y:68 -2020-02-10 10:20:17,713 # X: 169, Y:69 -2020-02-10 10:20:17,725 # X: 170, Y:69 -2020-02-10 10:20:17,737 # X: 170, Y:70 -2020-02-10 10:20:17,755 # X: 173, Y:69 -2020-02-10 10:20:17,761 # Released! +2020-07-21 21:24:49,286 # Pressed! +2020-07-21 21:24:49,293 # X: 132, Y:138 +2020-07-21 21:24:49,826 # Released! +2020-07-21 21:24:51,218 # Pressed! +2020-07-21 21:24:51,219 # X: 42, Y:16 +2020-07-21 21:24:51,614 # Released! +2020-07-21 21:24:53,385 # Pressed! +2020-07-21 21:24:53,385 # X: 197, Y:64 +2020-07-21 21:24:53,588 # Released! ```