Skip to content

Commit 1b784f9

Browse files
kuligakartben
authored andcommitted
drivers: auxdisplay: hd44780: rework the initialization procedure
If proper power supply is used to power the hd44780, it initializes correctly on a reset condition all by itself. However, if the power supply is below its expectations (e.g. some 3.3V Nucleo board), it won't initialize properly on its own, and the MCU has to carry out the initialization as listed in the reference manual. Since we cannot determine it properly in the runtime, always carry out the initialization procedure. Signed-off-by: Jan Kuliga <[email protected]>
1 parent 91a14dc commit 1b784f9

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

drivers/auxdisplay/auxdisplay_hd44780.c

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct auxdisplay_hd44780_config {
7777

7878
static void auxdisplay_hd44780_set_entry_mode(const struct device *dev);
7979
static void auxdisplay_hd44780_set_display_mode(const struct device *dev, bool enabled);
80+
static int auxdisplay_hd44780_clear(const struct device *dev);
8081

8182
static void hd44780_pulse_enable_line(const struct device *dev)
8283
{
@@ -109,13 +110,53 @@ static void auxdisplay_hd44780_command(const struct device *dev, bool rs, uint8_
109110
}
110111
}
111112

113+
static void hd44780_ic_initialize(const struct device *dev)
114+
{
115+
const struct auxdisplay_hd44780_config *config = dev->config;
116+
uint8_t cmd;
117+
118+
/*
119+
* If proper power supply is used to power the HD44780, it initializes correctly
120+
* on a reset condition all by itself. However, if the power supply is below
121+
* its expectations (e.g. supplying it with some 3.3V Nucleo board),
122+
* it won't initialize properly on its own, and the MCU has to carry out
123+
* the initialization as listed in the reference manual.
124+
* Since we cannot determine it properly in the runtime,
125+
* always carry out the initialization procedure.
126+
*/
127+
cmd = AUXDISPLAY_HD44780_CMD_SETUP | AUXDISPLAY_HD44780_8_BIT_CONFIG;
128+
auxdisplay_hd44780_command(dev, false, cmd, AUXDISPLAY_HD44780_MODE_4_BIT_ONCE);
129+
k_sleep(K_USEC(4100));
130+
auxdisplay_hd44780_command(dev, false, cmd, AUXDISPLAY_HD44780_MODE_4_BIT_ONCE);
131+
k_sleep(K_USEC(100));
132+
auxdisplay_hd44780_command(dev, false, cmd, AUXDISPLAY_HD44780_MODE_4_BIT_ONCE);
133+
k_sleep(K_USEC(100));
134+
135+
if (config->capabilities.mode == AUXDISPLAY_HD44780_MODE_4_BIT) {
136+
/* Put display into 4-bit mode */
137+
cmd = AUXDISPLAY_HD44780_CMD_SETUP;
138+
auxdisplay_hd44780_command(dev, false, cmd, AUXDISPLAY_HD44780_MODE_4_BIT_ONCE);
139+
}
140+
141+
/* Configure display */
142+
if (config->capabilities.rows > 1) {
143+
cmd |= AUXDISPLAY_HD44780_2_LINE_CONFIG;
144+
}
145+
auxdisplay_hd44780_command(dev, false, cmd, config->capabilities.mode);
146+
147+
auxdisplay_hd44780_set_display_mode(dev, false);
148+
auxdisplay_hd44780_clear(dev);
149+
auxdisplay_hd44780_set_entry_mode(dev);
150+
151+
auxdisplay_hd44780_set_display_mode(dev, true);
152+
}
153+
112154
static int auxdisplay_hd44780_init(const struct device *dev)
113155
{
114156
const struct auxdisplay_hd44780_config *config = dev->config;
115157
struct auxdisplay_hd44780_data *data = dev->data;
116158
int rc;
117159
uint8_t i = 0;
118-
uint8_t cmd = AUXDISPLAY_HD44780_CMD_SETUP | AUXDISPLAY_HD44780_8_BIT_CONFIG;
119160

120161
if (config->capabilities.mode > AUXDISPLAY_HD44780_MODE_8_BIT) {
121162
/* This index is reserved for internal driver usage */
@@ -199,28 +240,7 @@ static int auxdisplay_hd44780_init(const struct device *dev)
199240
k_sleep(K_MSEC(config->boot_delay));
200241
}
201242

202-
if (config->capabilities.mode == AUXDISPLAY_HD44780_MODE_4_BIT) {
203-
/* Reset display to known state in 8-bit mode */
204-
auxdisplay_hd44780_command(dev, false, cmd, AUXDISPLAY_HD44780_MODE_4_BIT_ONCE);
205-
auxdisplay_hd44780_command(dev, false, cmd, AUXDISPLAY_HD44780_MODE_4_BIT_ONCE);
206-
207-
/* Put display into 4-bit mode */
208-
cmd = AUXDISPLAY_HD44780_CMD_SETUP;
209-
auxdisplay_hd44780_command(dev, false, cmd, AUXDISPLAY_HD44780_MODE_4_BIT_ONCE);
210-
}
211-
212-
if (config->capabilities.rows > 1) {
213-
cmd |= AUXDISPLAY_HD44780_2_LINE_CONFIG;
214-
}
215-
216-
/* Configure display */
217-
auxdisplay_hd44780_command(dev, false, cmd, config->capabilities.mode);
218-
auxdisplay_hd44780_set_display_mode(dev, true);
219-
auxdisplay_hd44780_set_entry_mode(dev);
220-
auxdisplay_hd44780_command(dev, false, AUXDISPLAY_HD44780_CMD_CLEAR,
221-
config->capabilities.mode);
222-
223-
k_sleep(K_USEC(config->clear_delay));
243+
hd44780_ic_initialize(dev);
224244

225245
return 0;
226246
}

0 commit comments

Comments
 (0)