From d3330e1813e9c53906c8f423f9ed6e43ba9cea5f Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Sun, 2 Jan 2022 07:59:38 +0100 Subject: [PATCH 1/5] drivers/cst816s: migrate to ztimer --- drivers/cst816s/Makefile.dep | 3 ++- drivers/cst816s/cst816s.c | 6 +++--- drivers/cst816s/include/cst816s_internal.h | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/cst816s/Makefile.dep b/drivers/cst816s/Makefile.dep index ac9f3060a5..b8ca432d94 100644 --- a/drivers/cst816s/Makefile.dep +++ b/drivers/cst816s/Makefile.dep @@ -1,3 +1,4 @@ FEATURES_REQUIRED += periph_gpio_irq FEATURES_REQUIRED += periph_i2c -USEMODULE += xtimer +USEMODULE += ztimer +USEMODULE += ztimer_msec diff --git a/drivers/cst816s/cst816s.c b/drivers/cst816s/cst816s.c index 6aecd207c2..38a3aab11c 100644 --- a/drivers/cst816s/cst816s.c +++ b/drivers/cst816s/cst816s.c @@ -21,7 +21,7 @@ #include "log.h" #include "periph/gpio.h" #include "periph/i2c.h" -#include "xtimer.h" +#include "ztimer.h" #include "cst816s.h" #include "cst816s_internal.h" @@ -55,9 +55,9 @@ static void _cst816s_reset(const cst816s_t *dev) /* Reset, sleep durations based on * https://github.com/lupyuen/hynitron_i2c_cst0xxse/blob/master/cst0xx_core.c#L1078-L1085 */ gpio_clear(dev->params->reset); - xtimer_usleep(CST816S_RESET_DURATION_LOW); + ztimer_sleep(ZTIMER_MSEC, CST816S_RESET_DURATION_LOW_MS); gpio_set(dev->params->reset); - xtimer_usleep(CST816S_RESET_DURATION_HIGH); + ztimer_sleep(ZTIMER_MSEC, CST816S_RESET_DURATION_HIGH_MS); } int cst816s_read(const cst816s_t *dev, cst816s_touch_data_t *data) diff --git a/drivers/cst816s/include/cst816s_internal.h b/drivers/cst816s/include/cst816s_internal.h index 24dec4886d..bde0bd6650 100644 --- a/drivers/cst816s/include/cst816s_internal.h +++ b/drivers/cst816s/include/cst816s_internal.h @@ -25,8 +25,8 @@ extern "C" { * @name cst816s timing constants * @{ */ -#define CST816S_RESET_DURATION_LOW (20 * US_PER_MS) -#define CST816S_RESET_DURATION_HIGH (400 * US_PER_MS) +#define CST816S_RESET_DURATION_LOW_MS (20) /**< Duration of reset pin low (in ms) */ +#define CST816S_RESET_DURATION_HIGH_MS (400) /**< Duration of reset pin high (in ms) */ /** @} */ #ifdef __cplusplus From b1ac9e124d81707179b906990e4b0eb96536d3bc Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Sun, 2 Jan 2022 08:01:03 +0100 Subject: [PATCH 2/5] drivers/cst816s: add Kconfig --- drivers/Kconfig | 1 + drivers/cst816s/Kconfig | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/Kconfig b/drivers/Kconfig index 9410f47601..4e527e6562 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -77,6 +77,7 @@ rsource "bmp180/Kconfig" rsource "bmx055/Kconfig" rsource "bmx280/Kconfig" rsource "ccs811/Kconfig" +rsource "cst816s/Kconfig" rsource "dcf77/Kconfig" rsource "dht/Kconfig" rsource "ds18/Kconfig" diff --git a/drivers/cst816s/Kconfig b/drivers/cst816s/Kconfig index 63de11b005..1fc3564d40 100644 --- a/drivers/cst816s/Kconfig +++ b/drivers/cst816s/Kconfig @@ -12,9 +12,11 @@ config MODULE_CST816S depends on TEST_KCONFIG select MODULE_PERIPH_GPIO_IRQ select MODULE_PERIPH_I2C - select MODULE_XTIMER + select MODULE_ZTIMER + select MODULE_ZTIMER_MSEC config HAVE_CST816S bool + select MODULE_CST816S if MODULE_TOUCH_DEV help Indicates that a cst816s touch screen is present. From a2fc925a0e4bfde259ada1d6f41c50fc4fa47c5c Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Sun, 2 Jan 2022 08:01:31 +0100 Subject: [PATCH 3/5] drivers/cst816s: add touch_dev interface --- drivers/cst816s/Makefile | 2 +- drivers/cst816s/cst816s_touch_dev.c | 93 +++++++++++++++++++++ drivers/cst816s/include/cst816s_params.h | 15 ++++ drivers/cst816s/include/cst816s_touch_dev.h | 38 +++++++++ drivers/include/cst816s.h | 7 ++ sys/auto_init/screen/auto_init_cst816s.c | 50 +++++++++++ sys/auto_init/screen/init.c | 4 + 7 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 drivers/cst816s/cst816s_touch_dev.c create mode 100644 drivers/cst816s/include/cst816s_touch_dev.h create mode 100644 sys/auto_init/screen/auto_init_cst816s.c diff --git a/drivers/cst816s/Makefile b/drivers/cst816s/Makefile index 48422e909a..834f121424 100644 --- a/drivers/cst816s/Makefile +++ b/drivers/cst816s/Makefile @@ -1 +1 @@ -include $(RIOTBASE)/Makefile.base +include $(RIOTMAKE)/driver_with_touch_dev.mk diff --git a/drivers/cst816s/cst816s_touch_dev.c b/drivers/cst816s/cst816s_touch_dev.c new file mode 100644 index 0000000000..1782195277 --- /dev/null +++ b/drivers/cst816s/cst816s_touch_dev.c @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2022 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_cst816s + * @{ + * + * @file + * @brief Driver adaption to touch_dev generic interface + * + * @author Alexandre Abadie + * @} + */ + +#include +#include +#include +#include + +#include "kernel_defines.h" + +#include "board.h" +#include "cst816s.h" +#include "cst816s_touch_dev.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +#ifndef CST816S_XMAX +#define CST816S_XMAX 240 +#endif + +#ifndef CST816S_YMAX +#define CST816S_YMAX 240 +#endif + +uint16_t _cst816s_height(const touch_dev_t *touch_dev) +{ + const cst816s_t *dev = (const cst816s_t *)touch_dev; + assert(dev); + + return CST816S_YMAX; +} + +uint16_t _cst816s_width(const touch_dev_t *touch_dev) +{ + const cst816s_t *dev = (const cst816s_t *)touch_dev; + assert(dev); + + return CST816S_XMAX; +} + +uint8_t _cst816s_touches(const touch_dev_t *touch_dev, touch_t *touches, size_t len) +{ + (void)len; + + cst816s_t *dev = (cst816s_t *)touch_dev; + assert(dev); + + cst816s_touch_data_t data; + cst816s_read(dev, &data); + uint8_t ret = (data.action == CST816S_TOUCH_DOWN); + + if (ret && touches != NULL) { + touches[0].x = data.x; + touches[0].y = data.y; + + DEBUG("X: %i, Y: %i\n", touches[0].x, touches[0].y); + } + + return ret; +} + +void _cst816s_set_event_callback(const touch_dev_t *touch_dev, touch_event_cb_t cb, void *arg) +{ + cst816s_t *dev = (cst816s_t *)touch_dev; + assert(dev); + + dev->cb = (cst816s_irq_cb_t)cb; + dev->cb_arg = arg; +} + +const touch_dev_driver_t cst816s_touch_dev_driver = { + .height = _cst816s_height, + .width = _cst816s_width, + .touches = _cst816s_touches, + .set_event_callback = _cst816s_set_event_callback, +}; diff --git a/drivers/cst816s/include/cst816s_params.h b/drivers/cst816s/include/cst816s_params.h index 83791eb9a6..af87ef50e6 100644 --- a/drivers/cst816s/include/cst816s_params.h +++ b/drivers/cst816s/include/cst816s_params.h @@ -75,6 +75,21 @@ static const cst816s_params_t cst816s_params[] = */ #define CST816S_NUMOF ARRAY_SIZE(cst816s_params) +/** + * @brief Default screen identifiers + */ +#ifndef CST816S_PARAM_SCREEN_IDS +#define CST816S_PARAM_SCREEN_IDS 0 +#endif + +/** + * @brief Configure screen identifiers + */ +static const uint8_t cst816s_screen_ids[] = +{ + CST816S_PARAM_SCREEN_IDS, +}; + #ifdef __cplusplus } #endif diff --git a/drivers/cst816s/include/cst816s_touch_dev.h b/drivers/cst816s/include/cst816s_touch_dev.h new file mode 100644 index 0000000000..6bbfa10278 --- /dev/null +++ b/drivers/cst816s/include/cst816s_touch_dev.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2022 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_cst816s + * @{ + * + * @file + * @brief Definition of the driver for the touch_dev generic interface + * + * @author Alexandre Abadie + */ + +#ifndef CST816S_TOUCH_DEV_H +#define CST816S_TOUCH_DEV_H + +#include "touch_dev.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Reference to the touch device driver struct + */ +extern const touch_dev_driver_t cst816s_touch_dev_driver; + +#ifdef __cplusplus +} +#endif + +#endif /* CST816S_TOUCH_DEV_H */ +/** @} */ diff --git a/drivers/include/cst816s.h b/drivers/include/cst816s.h index 9a21ca2318..6c8f47b573 100644 --- a/drivers/include/cst816s.h +++ b/drivers/include/cst816s.h @@ -46,6 +46,10 @@ #include "periph/i2c.h" #include "periph/gpio.h" +#ifdef MODULE_TOUCH_DEV +#include "touch_dev.h" +#endif + #ifdef __cplusplus extern "C" { #endif @@ -108,6 +112,9 @@ typedef struct { * @brief cst816s device descriptor */ typedef struct { +#ifdef MODULE_TOUCH_DEV + touch_dev_t *dev; /**< Pointer to the generic touch device */ +#endif const cst816s_params_t *params; /**< Device parameters */ cst816s_irq_cb_t cb; /**< Configured IRQ event callback */ void *cb_arg; /**< Extra argument for the callback */ diff --git a/sys/auto_init/screen/auto_init_cst816s.c b/sys/auto_init/screen/auto_init_cst816s.c new file mode 100644 index 0000000000..457e2f37e5 --- /dev/null +++ b/sys/auto_init/screen/auto_init_cst816s.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2022 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_auto_init + * @{ + * @file + * @brief initializes cst816s display device + * + * @author Alexandre Abadie + * @} + */ + +#include + +#include "log.h" + +#include "touch_dev.h" + +#include "cst816s.h" +#include "cst816s_params.h" +#include "cst816s_touch_dev.h" + +cst816s_t cst816s_devs[CST816S_NUMOF]; +static touch_dev_reg_t touch_dev_entries[CST816S_NUMOF]; + +void auto_init_cst816s(void) +{ + assert(CST816S_NUMOF == ARRAY_SIZE(cst816s_screen_ids)); + + for (size_t i = 0; i < CST816S_NUMOF; i++) { + LOG_DEBUG("[auto_init_screen] initializing cst816s #%u\n", i); + if (cst816s_init(&cst816s_devs[i], &cst816s_params[i], NULL, NULL) < 0) { + LOG_ERROR("[auto_init_screen] error initializing cst816s #%u\n", i); + continue; + } + + touch_dev_entries[i].dev = (touch_dev_t *)&cst816s_devs[i]; + touch_dev_entries[i].screen_id = cst816s_screen_ids[i]; + touch_dev_entries[i].dev->driver = &cst816s_touch_dev_driver; + + /* add to touch_dev registry */ + touch_dev_reg_add(&(touch_dev_entries[i])); + } +} diff --git a/sys/auto_init/screen/init.c b/sys/auto_init/screen/init.c index 38d38c3a4e..c6ba9938b0 100644 --- a/sys/auto_init/screen/init.c +++ b/sys/auto_init/screen/init.c @@ -41,6 +41,10 @@ void auto_init_screen(void) if (IS_USED(MODULE_TOUCH_DEV)) { DEBUG("auto_init_screen: init touch drivers\n"); + if (IS_USED(MODULE_CST816S)) { + extern void auto_init_cst816s(void); + auto_init_cst816s(); + } if (IS_USED(MODULE_STMPE811)) { extern void auto_init_stmpe811(void); auto_init_stmpe811(); From 2d33b5f3a599ab952342cef020881d5a6e24fa03 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Sun, 2 Jan 2022 08:01:56 +0100 Subject: [PATCH 4/5] boards/pinetime: pull-in cst816s with touch_dev --- boards/pinetime/Kconfig | 1 + boards/pinetime/Makefile.dep | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/boards/pinetime/Kconfig b/boards/pinetime/Kconfig index a9a8ed1bce..2212fb4788 100644 --- a/boards/pinetime/Kconfig +++ b/boards/pinetime/Kconfig @@ -16,6 +16,7 @@ config BOARD_PINETIME select HAS_PERIPH_SPI select HAS_VDD_LC_FILTER_REG1 + select HAVE_CST816S select HAVE_ILI9341 select HAVE_MTD_SPI_NOR diff --git a/boards/pinetime/Makefile.dep b/boards/pinetime/Makefile.dep index aee3ae5527..331a4cf3e7 100644 --- a/boards/pinetime/Makefile.dep +++ b/boards/pinetime/Makefile.dep @@ -11,5 +11,9 @@ ifneq (,$(filter disp_dev,$(USEMODULE))) USEMODULE += ili9341 endif +ifneq (,$(filter touch_dev,$(USEMODULE))) + USEMODULE += cst816s +endif + # include common nrf52 dependencies include $(RIOTBOARD)/common/nrf52/Makefile.dep From dbc59aa75bce130c6d4152b4957f45f466918e48 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Fri, 25 Mar 2022 18:20:13 +0100 Subject: [PATCH 5/5] drivers/cst816s: use gpio_is_valid where appropriate --- drivers/cst816s/cst816s.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/cst816s/cst816s.c b/drivers/cst816s/cst816s.c index 38a3aab11c..72c1c4cbe7 100644 --- a/drivers/cst816s/cst816s.c +++ b/drivers/cst816s/cst816s.c @@ -89,12 +89,12 @@ int cst816s_init(cst816s_t *dev, const cst816s_params_t *params, dev->cb = cb; dev->cb_arg = arg; - if (dev->params->reset != GPIO_UNDEF) { + if (gpio_is_valid(dev->params->reset)) { gpio_init(dev->params->reset, GPIO_OUT); _cst816s_reset(dev); } - if ((dev->params->irq != GPIO_UNDEF) && cb) { + if (gpio_is_valid(dev->params->irq) && cb) { if (gpio_init_int(dev->params->irq, GPIO_IN, dev->params->irq_flank, _gpio_irq, dev) < 0) {