From 4667b296989dc19c0d6915c031f401a6f28542d5 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Thu, 1 Jul 2021 11:02:22 +0200 Subject: [PATCH] [color] fix attempt for GT911 touch sensor driver This fix contains 2 modifications: - poll GT911_READ_XY_REG for 20ms in case not yet ready - write 0 to GT911_READ_XY_REG in all cases These fixes are largely inspired from the Linux GT9xx driver (see https://gitlab.com/AdyaAdya/goodix-touchscreen-linux-driver/-/blob/master/goodix.c) --- radio/src/targets/horus/tp_gt911.cpp | 78 +++++++++++++++++----------- radio/src/targets/horus/tp_gt911.h | 7 ++- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/radio/src/targets/horus/tp_gt911.cpp b/radio/src/targets/horus/tp_gt911.cpp index 624cac3f8..d176b5b71 100644 --- a/radio/src/targets/horus/tp_gt911.cpp +++ b/radio/src/targets/horus/tp_gt911.cpp @@ -711,40 +711,56 @@ void touchPanelRead() if (!touchEventOccured) return; - touchEventOccured = false; - I2C_GT911_ReadRegister(GT911_READ_XY_REG, &state, 1); + touchEventOccured = 0; + + uint32_t startReadStatus = RTOS_GET_MS(); + do { + if (!I2C_GT911_ReadRegister(GT911_READ_XY_REG, &state, 1)) { + TRACE("GT911 I2C read error"); + return; + } + + if (state & 0x80u) { + // ready + break; + } + RTOS_WAIT_MS(1); + } while(RTOS_GET_MS() - startReadStatus < GT911_TIMEOUT); + TRACE("touch state = 0x%x", state); - if ((state & 0x80u) == 0x00) { - // not ready - return; - } + if (state & 0x80u) { + uint8_t pointsCount = (state & 0x0Fu); - uint8_t pointsCount = (state & 0x0Fu); - - if (pointsCount > 0 && pointsCount < GT911_MAX_TP) { - I2C_GT911_ReadRegister(GT911_READ_XY_REG + 1, touchData.data, pointsCount * sizeof(TouchPoint)); - if (touchState.event == TE_NONE || touchState.event == TE_UP || touchState.event == TE_SLIDE_END) { - touchState.event = TE_DOWN; - touchState.startX = touchState.x = touchData.points[0].x; - touchState.startY = touchState.y = touchData.points[0].y; - } - else { - touchState.deltaX = touchData.points[0].x - touchState.x; - touchState.deltaY = touchData.points[0].y - touchState.y; - if (touchState.event == TE_SLIDE || abs(touchState.deltaX) >= SLIDE_RANGE || abs(touchState.deltaY) >= SLIDE_RANGE) { - touchState.event = TE_SLIDE; - touchState.x = touchData.points[0].x; - touchState.y = touchData.points[0].y; + if (pointsCount > 0 && pointsCount <= GT911_MAX_TP) { + if (!I2C_GT911_ReadRegister(GT911_READ_XY_REG + 1, touchData.data, + pointsCount * sizeof(TouchPoint))) { + TRACE("GT911 I2C read error"); + return; + } + if (touchState.event == TE_NONE || touchState.event == TE_UP || + touchState.event == TE_SLIDE_END) { + touchState.event = TE_DOWN; + touchState.startX = touchState.x = touchData.points[0].x; + touchState.startY = touchState.y = touchData.points[0].y; + } else { + touchState.deltaX = touchData.points[0].x - touchState.x; + touchState.deltaY = touchData.points[0].y - touchState.y; + if (touchState.event == TE_SLIDE || + abs(touchState.deltaX) >= SLIDE_RANGE || + abs(touchState.deltaY) >= SLIDE_RANGE) { + touchState.event = TE_SLIDE; + touchState.x = touchData.points[0].x; + touchState.y = touchData.points[0].y; + } + } + } else { + if (touchState.event == TE_SLIDE) { + touchState.event = TE_SLIDE_END; + } else if (touchState.event == TE_DOWN) { + touchState.event = TE_UP; + } else if (touchState.event != TE_SLIDE_END) { + touchState.event = TE_NONE; } - } - } - else { - if (touchState.event == TE_SLIDE) { - touchState.event = TE_SLIDE_END; - } else if (touchState.event == TE_DOWN) { - touchState.event = TE_UP; - } else if (touchState.event != TE_SLIDE_END){ - touchState.event = TE_NONE; } } diff --git a/radio/src/targets/horus/tp_gt911.h b/radio/src/targets/horus/tp_gt911.h index edbf3fd57..1a3371d31 100644 --- a/radio/src/targets/horus/tp_gt911.h +++ b/radio/src/targets/horus/tp_gt911.h @@ -18,8 +18,7 @@ * GNU General Public License for more details. */ -#ifndef _FT5X06_H_ -#define _FT5X06_H_ +#pragma once #define HAS_TOUCH_PANEL() touchGT911Flag == true @@ -29,6 +28,8 @@ extern bool touchPanelInit(); void touchPanelRead(); bool touchPanelEventOccured(); +#define GT911_TIMEOUT 20 // 20ms + #define GT911_MAX_TP 5 #define GT911_CFG_NUMER 0x6C @@ -81,5 +82,3 @@ PACK(struct TouchData { #define TPINT_LOW() do { TOUCH_INT_GPIO->BSRRH = TOUCH_INT_GPIO_PIN; } while(0) #define TPINT_HIGH() do { TOUCH_INT_GPIO->BSRRL = TOUCH_INT_GPIO_PIN; } while(0) - -#endif // _FT5X06_H_ \ No newline at end of file