1 /*
2 * Copyright (c) 2015 Intel Corporation
3 * Copyright (c) 2022 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #define DT_DRV_COMPAT seeed_grove_lcd_rgb
9
10 #include <zephyr/device.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/drivers/misc/grove_lcd/grove_lcd.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/sys/util.h>
15
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_REGISTER(grove_lcd, CONFIG_GROVE_LCD_RGB_LOG_LEVEL);
18
19 #define GROVE_RGB_BACKLIGHT_ADDR (0x62)
20
21 struct glcd_data {
22 uint8_t input_set;
23 uint8_t display_switch;
24 uint8_t function;
25 };
26
27 struct glcd_config {
28 struct i2c_dt_spec bus;
29 };
30
31 /********************************************
32 * LCD FUNCTIONS
33 *******************************************/
34
35 /* GLCD_CMD_SCREEN_CLEAR has no options */
36 /* GLCD_CMD_CURSOR_RETURN has no options */
37
38 /* Defines for the GLCD_CMD_CURSOR_SHIFT */
39 #define GLCD_CS_DISPLAY_SHIFT (1 << 3)
40 #define GLCD_CS_RIGHT_SHIFT (1 << 2)
41
42 /* LCD Display Commands */
43 #define GLCD_CMD_SCREEN_CLEAR (1 << 0)
44 #define GLCD_CMD_CURSOR_RETURN (1 << 1)
45 #define GLCD_CMD_INPUT_SET (1 << 2)
46 #define GLCD_CMD_DISPLAY_SWITCH (1 << 3)
47 #define GLCD_CMD_CURSOR_SHIFT (1 << 4)
48 #define GLCD_CMD_FUNCTION_SET (1 << 5)
49 #define GLCD_CMD_SET_CGRAM_ADDR (1 << 6)
50 #define GLCD_CMD_SET_DDRAM_ADDR (1 << 7)
51
52
53 /********************************************
54 * RGB FUNCTIONS
55 *******************************************/
56
57 #define REGISTER_R 0x04
58 #define REGISTER_G 0x03
59 #define REGISTER_B 0x02
60
61 static uint8_t color_define[][3] = {
62 { 255, 255, 255 }, /* white */
63 { 255, 0, 0 }, /* red */
64 { 0, 255, 0 }, /* green */
65 { 0, 0, 255 }, /* blue */
66 };
67
68
69 /********************************************
70 * PRIVATE FUNCTIONS
71 *******************************************/
rgb_reg_set(const struct device * i2c,uint8_t addr,uint8_t dta)72 static void rgb_reg_set(const struct device *i2c, uint8_t addr, uint8_t dta)
73 {
74 uint8_t data[2] = { addr, dta };
75
76 i2c_write(i2c, data, sizeof(data), GROVE_RGB_BACKLIGHT_ADDR);
77 }
78
79 /********************************************
80 * PUBLIC FUNCTIONS
81 *******************************************/
glcd_print(const struct device * dev,char * data,uint32_t size)82 void glcd_print(const struct device *dev, char *data, uint32_t size)
83 {
84 const struct glcd_config *config = dev->config;
85 uint8_t buf[] = { GLCD_CMD_SET_CGRAM_ADDR, 0 };
86 int i;
87
88 for (i = 0; i < size; i++) {
89 buf[1] = data[i];
90 i2c_write_dt(&config->bus, buf, sizeof(buf));
91 }
92 }
93
94
glcd_cursor_pos_set(const struct device * dev,uint8_t col,uint8_t row)95 void glcd_cursor_pos_set(const struct device *dev, uint8_t col, uint8_t row)
96 {
97 const struct glcd_config *config = dev->config;
98
99 unsigned char data[2];
100
101 if (row == 0U) {
102 col |= 0x80;
103 } else {
104 col |= 0xC0;
105 }
106
107 data[0] = GLCD_CMD_SET_DDRAM_ADDR;
108 data[1] = col;
109
110 i2c_write_dt(&config->bus, data, 2);
111 }
112
113
glcd_clear(const struct device * dev)114 void glcd_clear(const struct device *dev)
115 {
116 const struct glcd_config *config = dev->config;
117 uint8_t clear[] = { 0, GLCD_CMD_SCREEN_CLEAR };
118
119 i2c_write_dt(&config->bus, clear, sizeof(clear));
120 LOG_DBG("clear, delay 20 ms");
121 k_sleep(K_MSEC(20));
122 }
123
124
glcd_display_state_set(const struct device * dev,uint8_t opt)125 void glcd_display_state_set(const struct device *dev, uint8_t opt)
126 {
127 const struct glcd_config *config = dev->config;
128 struct glcd_data *data = dev->data;
129 uint8_t buf[] = { 0, 0 };
130
131 data->display_switch = opt;
132 buf[1] = (opt | GLCD_CMD_DISPLAY_SWITCH);
133
134 i2c_write_dt(&config->bus, buf, sizeof(buf));
135
136 LOG_DBG("set display_state options, delay 5 ms");
137 k_sleep(K_MSEC(5));
138 }
139
140
glcd_display_state_get(const struct device * dev)141 uint8_t glcd_display_state_get(const struct device *dev)
142 {
143 struct glcd_data *data = dev->data;
144
145 return data->display_switch;
146 }
147
148
glcd_input_state_set(const struct device * dev,uint8_t opt)149 void glcd_input_state_set(const struct device *dev, uint8_t opt)
150 {
151 const struct glcd_config *config = dev->config;
152 struct glcd_data *data = dev->data;
153 uint8_t buf[] = { 0, 0 };
154
155 data->input_set = opt;
156 buf[1] = (opt | GLCD_CMD_INPUT_SET);
157
158 i2c_write_dt(&config->bus, buf, sizeof(buf));
159 LOG_DBG("set the input_set, no delay");
160 }
161
162
glcd_input_state_get(const struct device * dev)163 uint8_t glcd_input_state_get(const struct device *dev)
164 {
165 struct glcd_data *data = dev->data;
166
167 return data->input_set;
168 }
169
170
glcd_color_select(const struct device * dev,uint8_t color)171 void glcd_color_select(const struct device *dev, uint8_t color)
172 {
173 if (color > 3) {
174 LOG_WRN("selected color is too high a value");
175 return;
176 }
177 glcd_color_set(dev, color_define[color][0], color_define[color][1],
178 color_define[color][2]);
179 }
180
181
glcd_color_set(const struct device * dev,uint8_t r,uint8_t g,uint8_t b)182 void glcd_color_set(const struct device *dev, uint8_t r, uint8_t g,
183 uint8_t b)
184 {
185 const struct glcd_config *config = dev->config;
186
187 rgb_reg_set(config->bus.bus, REGISTER_R, r);
188 rgb_reg_set(config->bus.bus, REGISTER_G, g);
189 rgb_reg_set(config->bus.bus, REGISTER_B, b);
190 }
191
192
glcd_function_set(const struct device * dev,uint8_t opt)193 void glcd_function_set(const struct device *dev, uint8_t opt)
194 {
195 const struct glcd_config *config = dev->config;
196 struct glcd_data *data = dev->data;
197 uint8_t buf[] = { 0, 0 };
198
199 data->function = opt;
200 buf[1] = (opt | GLCD_CMD_FUNCTION_SET);
201
202 i2c_write_dt(&config->bus, buf, sizeof(buf));
203
204 LOG_DBG("set function options, delay 5 ms");
205 k_sleep(K_MSEC(5));
206 }
207
208
glcd_function_get(const struct device * dev)209 uint8_t glcd_function_get(const struct device *dev)
210 {
211 struct glcd_data *data = dev->data;
212
213 return data->function;
214 }
215
216
glcd_initialize(const struct device * dev)217 static int glcd_initialize(const struct device *dev)
218 {
219 const struct glcd_config *config = dev->config;
220 uint8_t cmd;
221
222 LOG_DBG("initialize called");
223
224 if (!device_is_ready(config->bus.bus)) {
225 return -ENODEV;
226 }
227
228 /*
229 * Initialization sequence from the data sheet:
230 * 1 - Power on
231 * - Wait for more than 30 ms AFTER VDD rises to 4.5v
232 * 2 - Send FUNCTION set
233 * - Wait for 39 us
234 * 3 - Send DISPLAY Control
235 * - wait for 39 us
236 * 4 - send DISPLAY Clear
237 * - wait for 1.5 ms
238 * 5 - send ENTRY Mode
239 * 6 - Initialization is done
240 */
241
242
243 /*
244 * We're here! Let's just make sure we've had enough time for the
245 * VDD to power on, so pause a little here, 30 ms min, so we go 50
246 */
247 LOG_DBG("delay 50 ms while the VDD powers on");
248 k_sleep(K_MSEC(50));
249
250 /* Configure everything for the display function first */
251 cmd = GLCD_CMD_FUNCTION_SET | GLCD_FS_ROWS_2;
252 glcd_function_set(dev, cmd);
253
254 /* turn the display on - by default no cursor and no blinking */
255 cmd = GLCD_DS_DISPLAY_ON | GLCD_DS_CURSOR_OFF | GLCD_DS_BLINK_OFF;
256
257 glcd_display_state_set(dev, cmd);
258
259 /* Clear the screen */
260 glcd_clear(dev);
261
262 /* Initialize to the default text direction for romance languages */
263 cmd = GLCD_IS_ENTRY_LEFT | GLCD_IS_SHIFT_DECREMENT;
264
265 glcd_input_state_set(dev, cmd);
266
267 /* Now power on the background RGB control */
268 LOG_INF("configuring the RGB background");
269 rgb_reg_set(config->bus.bus, 0x00, 0x00);
270 rgb_reg_set(config->bus.bus, 0x01, 0x05);
271 rgb_reg_set(config->bus.bus, 0x08, 0xAA);
272
273 /* Now set the background color to white */
274 LOG_DBG("background set to white");
275 rgb_reg_set(config->bus.bus, REGISTER_R, color_define[GROVE_RGB_WHITE][0]);
276 rgb_reg_set(config->bus.bus, REGISTER_G, color_define[GROVE_RGB_WHITE][1]);
277 rgb_reg_set(config->bus.bus, REGISTER_B, color_define[GROVE_RGB_WHITE][2]);
278
279 return 0;
280 }
281
282 static const struct glcd_config grove_lcd_config = {
283 .bus = I2C_DT_SPEC_INST_GET(0),
284 };
285
286 static struct glcd_data grove_lcd_data;
287
288 DEVICE_DT_INST_DEFINE(0, glcd_initialize, NULL, &grove_lcd_data,
289 &grove_lcd_config, POST_KERNEL,
290 CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);
291