From c7fac09d42222391415733e1ad6fa2bba16f71b4 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Wed, 6 Nov 2019 16:14:39 +0100 Subject: [PATCH 1/3] drivers/hd44780: fix potential hardfault on initialization Co-authored-by Benjamin Valentin --- drivers/hd44780/hd44780.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/hd44780/hd44780.c b/drivers/hd44780/hd44780.c index 73b2a60729d2..4895c9b9db6b 100644 --- a/drivers/hd44780/hd44780.c +++ b/drivers/hd44780/hd44780.c @@ -118,6 +118,7 @@ int hd44780_init(hd44780_t *dev, const hd44780_params_t *params) } } /* set mode depending on configured pins */ + dev->flag = 0; if (count_pins < HD44780_MAX_PINS) { dev->flag |= HD44780_4BITMODE; } From 6c56972dbae6d24edade1dfefcc9e013ab47ce97 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Wed, 6 Nov 2019 16:25:27 +0100 Subject: [PATCH 2/3] drivers/include/hd44780: avoid use of magic numbers --- drivers/include/hd44780.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/include/hd44780.h b/drivers/include/hd44780.h index 1b1da81ebd1b..ef611b849945 100644 --- a/drivers/include/hd44780.h +++ b/drivers/include/hd44780.h @@ -63,7 +63,7 @@ typedef struct { gpio_t rs; /**< rs gpio pin */ gpio_t rw; /**< rw gpio pin */ gpio_t enable; /**< enable gpio pin */ - gpio_t data[8]; /**< data gpio pins */ + gpio_t data[HD44780_MAX_PINS]; /**< data gpio pins */ } hd44780_params_t; /** From 62d299459fd2d14344b20a15e2016ac7f4ac5433 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Wed, 6 Nov 2019 16:27:39 +0100 Subject: [PATCH 3/3] drivers/hd44780: optimize detection of 4 and 8 bit modes The driver can only be used with either 4 or 8 bit modes. Checking if the 5th pin is set in the configuration is enough the determine if 8bit mode should be used or not --- drivers/hd44780/hd44780.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/drivers/hd44780/hd44780.c b/drivers/hd44780/hd44780.c index 4895c9b9db6b..dc6e2ba5affa 100644 --- a/drivers/hd44780/hd44780.c +++ b/drivers/hd44780/hd44780.c @@ -110,20 +110,13 @@ int hd44780_init(hd44780_t *dev, const hd44780_params_t *params) LOG_ERROR("hd44780_init: invalid LCD size!\n"); return -1; } - uint8_t count_pins = 0; - /* check which pins are used */ - for (unsigned i = 0; i < HD44780_MAX_PINS; ++i) { - if (dev->p.data[i] != GPIO_UNDEF) { - ++count_pins; - } - } - /* set mode depending on configured pins */ dev->flag = 0; - if (count_pins < HD44780_MAX_PINS) { - dev->flag |= HD44780_4BITMODE; + /* set mode depending on configured pins */ + if (dev->p.data[4] != GPIO_UNDEF) { + dev->flag |= HD44780_8BITMODE; } else { - dev->flag |= HD44780_8BITMODE; + dev->flag |= HD44780_4BITMODE; } /* set flag for 1 or 2 row mode, 4 rows are 2 rows split half */ if (dev->p.rows > 1) {