From 61d87f57f8f56d268eba4a8e4d16d52af5bb0605 Mon Sep 17 00:00:00 2001 From: Dan Kalowsky Date: Thu, 15 Oct 2015 11:00:07 -0700 Subject: [PATCH] grove-lcd: import of Grove LCD I2C controller Initial import of the Grove LCD I2C controller. Data sheet can be found at: http://www.seeedstudio.com/wiki/images/0/03/JHD1214Y_YG_1.0.pdf [DL: Updated command sequence according to datasheet.] Change-Id: Id1b491f8dce346769dc42c41fac0ea3aabe3950a Signed-off-by: Daniel Leung Signed-off-by: Dan Kalowsky --- drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/grove/Kconfig | 53 +++++ drivers/grove/Makefile | 1 + drivers/grove/lcd_rgb.c | 373 ++++++++++++++++++++++++++++++++++++ include/display/grove_lcd.h | 168 ++++++++++++++++ 6 files changed, 598 insertions(+) create mode 100644 drivers/grove/Kconfig create mode 100644 drivers/grove/Makefile create mode 100644 drivers/grove/lcd_rgb.c create mode 100644 include/display/grove_lcd.h diff --git a/drivers/Kconfig b/drivers/Kconfig index d323297ac38..ca1fa195f7d 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -33,6 +33,8 @@ source "drivers/timer/Kconfig" source "drivers/random/Kconfig" +source "drivers/grove/Kconfig" + source "drivers/pci/Kconfig" source "drivers/gpio/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index d26b2b5cbe0..ed7e3c9455f 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -7,6 +7,7 @@ obj-y += random/ obj-y += serial/ obj-y += timer/ obj-y += interrupt_controller/ +obj-$(CONFIG_GROVE) += grove/ obj-$(CONFIG_PCI) += pci/ obj-$(CONFIG_BLUETOOTH) += bluetooth/ obj-$(CONFIG_UART_SIMPLE) += simple/ diff --git a/drivers/grove/Kconfig b/drivers/grove/Kconfig new file mode 100644 index 00000000000..f3956ed7395 --- /dev/null +++ b/drivers/grove/Kconfig @@ -0,0 +1,53 @@ +# Kconfig - drivers configuration options for SeeedStudio Grove Devices + +# +# Copyright (c) 2015 Intel Corporation +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1) Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2) Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# 3) Neither the name of Intel Corporation nor the names of its contributors +# may be used to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + + +menuconfig GROVE + bool + prompt "Grove Device Drivers" + default n + help + Check this box to enable the Seeed Grove device drivers + +config GROVE_DEBUG + bool + prompt "Enable Grove debug messagings" + default n + +config GROVE_LCD_RGB + bool + default n + depends on GROVE + prompt "Enable the Seeed Grove LCD RGB Backlight" + help + Setting this value will enable driver support for the Groove-LCD RGB + Backlight. diff --git a/drivers/grove/Makefile b/drivers/grove/Makefile new file mode 100644 index 00000000000..499e25753d9 --- /dev/null +++ b/drivers/grove/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_GROVE_LCD_RGB) += lcd_rgb.o diff --git a/drivers/grove/lcd_rgb.c b/drivers/grove/lcd_rgb.c new file mode 100644 index 00000000000..56c035bf804 --- /dev/null +++ b/drivers/grove/lcd_rgb.c @@ -0,0 +1,373 @@ +/* + * Copyright (c) 2015 Intel Corporation + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2) Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3) Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include + +#include +#include +#include + +#include +#include +#include + +#define SLEEP_IN_US(_x_) ((_x_) * 1000) + +#define GROVE_LCD_DISPLAY_ADDR (0x3E) +#define GROVE_RGB_BACKLIGHT_ADDR (0x62) + +#ifndef CONFIG_GROVE_DEBUG +#define DBG(...) { ; } +#else +#if defined(CONFIG_STDOUT_CONSOLE) +#include +#define DBG printf +#else +#include +#define DBG printk +#endif /* CONFIG_STDOUT_CONSOLE */ +#endif /* CONFIG_GROVE_DEBUG */ + +struct command { + uint8_t control; + uint8_t data; +}; + +struct glcd_data { + struct device *i2c; + uint8_t input_set; + uint8_t display_switch; + uint8_t function; +}; + +struct glcd_driver { + uint16_t lcd_addr; + uint16_t rgb_addr; +}; + + +#define ON 0x1 +#define OFF 0x0 + +/******************************************** + * LCD FUNCTIONS + *******************************************/ + +/* GLCD_CMD_SCREEN_CLEAR has no options */ +/* GLCD_CMD_CURSOR_RETURN has no options */ + +/* Defines for the GLCD_CMD_CURSOR_SHIFT */ +#define GLCD_CS_DISPLAY_SHIFT (1 << 3) +#define GLCD_CS_RIGHT_SHIFT (1 << 2) + +/* LCD Display Commands */ +#define GLCD_CMD_SCREEN_CLEAR (1 << 0) +#define GLCD_CMD_CURSOR_RETURN (1 << 1) +#define GLCD_CMD_INPUT_SET (1 << 2) +#define GLCD_CMD_DISPLAY_SWITCH (1 << 3) +#define GLCD_CMD_CURSOR_SHIFT (1 << 4) +#define GLCD_CMD_FUNCTION_SET (1 << 5) +#define GLCD_CMD_SET_CGRAM_ADDR (1 << 6) +#define GLCD_CMD_SET_DDRAM_ADDR (1 << 7) + + +/******************************************** + * RGB FUNCTIONS + *******************************************/ + +#define REGISTER_POWER 0x08 +#define REGISTER_R 0x04 +#define REGISTER_G 0x03 +#define REGISTER_B 0x02 + +static uint8_t color_define[][3] = { + { 255, 255, 255 }, /* white */ + { 255, 0, 0 }, /* red */ + { 0, 255, 0 }, /* green */ + { 0, 0, 255 }, /* blue */ +}; + + +/******************************************** + * PRIVATE FUNCTIONS + *******************************************/ +static void _rgb_reg_set(struct device * const i2c, uint8_t addr, uint8_t dta) +{ + uint8_t data[2] = { addr, dta }; + + i2c_polling_write(i2c, data, sizeof(data), GROVE_RGB_BACKLIGHT_ADDR); +} + + +static inline void _sleep(uint32_t sleep_in_ms) +{ + sys_thread_busy_wait(SLEEP_IN_US(sleep_in_ms)); +} + + +/******************************************** + * PUBLIC FUNCTIONS + *******************************************/ +void glcd_print(struct device *port, unsigned char *data, uint32_t size) +{ + struct glcd_driver * const rom = (struct glcd_driver *) + port->config->config_info; + struct glcd_data *dev = port->driver_data; + uint8_t buf[] = { GLCD_CMD_SET_CGRAM_ADDR, 0 }; + int i; + + for (i = 0; i < size; i++) { + buf[1] = data[i]; + i2c_polling_write(dev->i2c, buf, sizeof(buf), rom->lcd_addr); + } +} + + +void glcd_cursor_pos_set(struct device *port, uint8_t col, uint8_t row) +{ + struct glcd_driver * const rom = (struct glcd_driver *) + port->config->config_info; + struct glcd_data *dev = port->driver_data; + + unsigned char data[2]; + + if (row == 0) { + col |= 0x80; + } else { + col |= 0xC0; + } + + data[0] = GLCD_CMD_SET_DDRAM_ADDR; + data[1] = col; + + i2c_polling_write(dev->i2c, data, 2, rom->lcd_addr); +} + + +void glcd_clear(struct device *port) +{ + struct glcd_driver * const rom = (struct glcd_driver *) + port->config->config_info; + struct glcd_data *dev = port->driver_data; + uint8_t clear[] = { 0, GLCD_CMD_SCREEN_CLEAR }; + + i2c_polling_write(dev->i2c, clear, sizeof(clear), rom->lcd_addr); + DBG("Grove LCD: clear, delay 20 ms\n"); + _sleep(20); +} + + +void glcd_display_state_set(struct device *port, uint8_t opt) +{ + struct glcd_driver * const rom = (struct glcd_driver *) + port->config->config_info; + struct glcd_data *dev = port->driver_data; + uint8_t data[] = { 0, 0 }; + + dev->display_switch = opt; + data[1] = (opt | GLCD_CMD_DISPLAY_SWITCH); + + i2c_polling_write(dev->i2c, data, sizeof(data), rom->lcd_addr); + + DBG("Grove LCD: set display_state options, delay 5 ms\n"); + _sleep(5); +} + + +uint8_t glcd_display_state_get(struct device *port) +{ + struct glcd_data *dev = port->driver_data; + + return dev->display_switch; +} + + +void glcd_input_state_set(struct device *port, uint8_t opt) +{ + struct glcd_driver * const rom = port->config->config_info; + struct glcd_data *dev = port->driver_data; + uint8_t data[] = { 0, 0 }; + + dev->input_set = opt; + data[1] = (opt | GLCD_CMD_INPUT_SET); + + i2c_polling_write(dev->i2c, &dev->input_set, sizeof(dev->input_set), + rom->lcd_addr); + DBG("Grove LCD: set the input_set, no delay\n"); +} + + +uint8_t glcd_input_state_get(struct device *port) +{ + struct glcd_data *dev = port->driver_data; + + return dev->input_set; +} + + +void glcd_color_select(struct device *port, uint8_t color) +{ +#if CONFIG_GROVE_DEBUG + if (color > 3) { + DBG("%s : selected color is too high a value\n", __func__); + return; + } +#endif + glcd_color_set(port, color_define[color][0], + color_define[color][1], + color_define[color][2]); +} + + +void glcd_color_set(struct device *port, uint8_t r, uint8_t g, uint8_t b) +{ + struct glcd_data * const dev = port->driver_data; + + _rgb_reg_set(dev->i2c, REGISTER_R, r); + _rgb_reg_set(dev->i2c, REGISTER_G, g); + _rgb_reg_set(dev->i2c, REGISTER_B, b); +} + + +void glcd_function_set(struct device *port, uint8_t opt) +{ + struct glcd_driver * const rom = port->config->config_info; + struct glcd_data *dev = port->driver_data; + uint8_t data[] = { 0, 0 }; + + dev->function = opt; + data[1] = (opt | GLCD_CMD_FUNCTION_SET); + + i2c_polling_write(dev->i2c, data, sizeof(data), rom->lcd_addr); + + DBG("Grove LCD: set function options, delay 5 ms\n"); + _sleep(5); +} + + +uint8_t glcd_function_get(struct device *port) +{ + struct glcd_data *dev = port->driver_data; + + return dev->function; +} + + +int glcd_initialize(struct device *port) +{ + struct glcd_data *dev = port->driver_data; + uint8_t cmd; + + DBG("Grove LCD: initialize called\n"); + + dev->input_set = 0; + dev->display_switch = 0; + dev->function = 0; + + /* + * First set up the device driver... + * we need a pointer to the I2C device we are bound to + */ + dev->i2c = device_get_binding(CONFIG_I2C_DW_0_NAME); + + if (!dev->i2c) { + return DEV_NOT_CONFIG; + } + + /* + * Initialization sequence from the data sheet: + * 1 - Power on + * - Wait for more than 30 ms AFTER VDD rises to 4.5v + * 2 - Send FUNCTION set + * - Wait for 39 us + * 3 - Send DISPLAY Control + * - wait for 39 us + * 4 - send DISPLAY Clear + * - wait for 1.5 ms + * 5 - send ENTRY Mode + * 6 - Initialization is done + */ + + + /* We're here! Let's just make sure we've had enough time for the + * VDD to power on, so pause a little here, 30 ms min, so we go 50 */ + DBG("Grove LCD: delay 50 ms while the VDD powers on\n"); + _sleep(50); + + /* Configure everything for the display function first */ + cmd = GLCD_CMD_FUNCTION_SET | GLCD_FS_ROWS_2; + glcd_function_set(port, cmd); + + /* turn the display on - by default no cursor and no blinking */ + cmd = GLCD_DS_DISPLAY_ON | GLCD_DS_CURSOR_OFF | GLCD_DS_BLINK_OFF; + + glcd_display_state_set(port, cmd); + + /* Clear the screen */ + glcd_clear(port); + + /* Initialize to the default text direction for romance languages */ + cmd = GLCD_IS_ENTRY_LEFT | GLCD_IS_SHIFT_DECREMENT; + + glcd_input_state_set(port, cmd); + + /* Now power on the background RGB control */ + DBG("Grove LCD: configuring the RGB background\n"); + _rgb_reg_set(dev->i2c, 0x00, 0x00); + _rgb_reg_set(dev->i2c, 0x01, 0x05); + _rgb_reg_set(dev->i2c, 0x08, 0xAA); + + /* Now set the background color to white */ + DBG("Grove LCD: background set to white\n"); + _rgb_reg_set(dev->i2c, REGISTER_R, color_define[GROVE_RGB_WHITE][0]); + _rgb_reg_set(dev->i2c, REGISTER_G, color_define[GROVE_RGB_WHITE][1]); + _rgb_reg_set(dev->i2c, REGISTER_B, color_define[GROVE_RGB_WHITE][2]); + + return DEV_OK; +} + +struct glcd_driver grove_lcd_config = { + .lcd_addr = GROVE_LCD_DISPLAY_ADDR, + .rgb_addr = GROVE_RGB_BACKLIGHT_ADDR, +}; + +struct glcd_data grove_lcd_driver = { + .i2c = NULL, + .input_set = 0, + .display_switch = 0, + .function = 0, +}; + +DECLARE_DEVICE_INIT_CONFIG(grove_lcd, + GROVE_LCD_NAME, + glcd_initialize, + &grove_lcd_config); + +app_early_init(grove_lcd, &grove_lcd_driver); diff --git a/include/display/grove_lcd.h b/include/display/grove_lcd.h new file mode 100644 index 00000000000..d17cbd71f51 --- /dev/null +++ b/include/display/grove_lcd.h @@ -0,0 +1,168 @@ +/* grove_lcd.h - Public API for the Grove RGB LCD device */ +/* + * Copyright (c) 2015 Intel Corporation + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2) Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3) Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef DISPLAY_GROVE_LCD_H +#define DISPLAY_GROVE_LCD_H + +#define GROVE_LCD_NAME "GLCD" + +/** + * @brief Send text to the screen + * @param port Pointer to device structure for driver instance. + * @param data the ASCII text to display + * @param size the length of the text in bytes + */ +void glcd_print(struct device *port, unsigned char *data, uint32_t size); + + +/** + * @brief Set text cursor position for next additions + * @param port Pointer to device structure for driver instance. + * @param col the column for the cursor to be moved to (0-15) + * @param row the row it should be moved to (0 or 1) + */ +void glcd_cursor_pos_set(struct device *port, uint8_t col, uint8_t row); + +/** + * @brief Clear the current display + * @param port Pointer to device structure for driver instance. + */ +void glcd_clear(struct device *port); + +/* Defines for the GLCD_CMD_DISPLAY_SWITCH options */ +#define GLCD_DS_DISPLAY_ON (1 << 2) +#define GLCD_DS_DISPLAY_OFF (0 << 2) +#define GLCD_DS_CURSOR_ON (1 << 1) +#define GLCD_DS_CURSOR_OFF (0 << 1) +#define GLCD_DS_BLINK_ON (1 << 0) +#define GLCD_DS_BLINK_OFF (0 << 0) +/** + * @brief Function to change the display state. + * @param port Pointer to device structure for driver instance. + * @param opt An 8bit bitmask of GLCD_DS_* options. + * + * @details This function provides the user the ability to change the state + * of the display as per needed. Controlling things like powering on or off + * the screen, the option to display the cusror or not, and the ability to + * blink the cursor. + */ +void glcd_display_state_set(struct device *port, uint8_t opt); + +/** + * @brief return the display feature set associated with the device + * @param the Grove LCD to get the display features set + * + * @return the display feature set associated with the device. + */ +uint8_t glcd_display_state_get(struct device *port); + +/* Defines for the GLCD_CMD_INPUT_SET to change text direction */ +#define GLCD_IS_SHIFT_INCREMENT (1 << 1) +#define GLCD_IS_SHIFT_DECREMENT (0 << 1) +#define GLCD_IS_ENTRY_LEFT (1 << 0) +#define GLCD_IS_ENTRY_RIGHT (0 << 0) +/** + * @brief Function to change the input state. + * @param port Pointer to device structure for driver instance. + * @param opt A bitmask of GLCD_IS_* options + * + * @details This function provides the user the ability to change the state + * of the text input. Controlling things like text entry from the left or + * right side, and how far to increment on new text + */ +void glcd_input_state_set(struct device *port, uint8_t opt); + +/** + * @brief return the input set associated with the device + * @param the Grove LCD to get the input features set + * + * @return the input set associated with the device. + */ +uint8_t glcd_input_state_get(struct device *port); + +/* Defines for the LCD_FUNCTION_SET */ +#define GLCD_FS_8BIT_MODE (1 << 4) +#define GLCD_FS_ROWS_2 (1 << 3) +#define GLCD_FS_ROWS_1 (0 << 3) +#define GLCD_FS_DOT_SIZE_BIG (1 << 2) +#define GLCD_FS_DOT_SIZE_LITTLE (0 << 2) +/* Bits 0, 1 are not defined for this register */ + +/** + * @brief Function to set the functional state of the display. + * @param port Pointer to device structure for driver instance. + * @param opt A bitmask of GLCD_FS_* options + * + * @details This function provides the user the ability to change the state + * of the display as per needed. Controlling things like the number of rows, + * dot size, and text display quality. + */ +void glcd_function_set(struct device *port, uint8_t opt); + +/** + * @brief return the function set associated with the device + * @param the Grove LCD to get the functions set + * + * @return the function features set associated with the device. + */ +uint8_t glcd_function_get(struct device *port); + + +/* Available color selections */ +#define GROVE_RGB_WHITE 0 +#define GROVE_RGB_RED 1 +#define GROVE_RGB_GREEN 2 +#define GROVE_RGB_BLUE 3 +/** + * @brief Set LCD background to a predfined color + * @param port Pointer to device structure for driver instance. + * @param color One of the pre-defined color options + */ +void glcd_color_select(struct device *port, uint8_t color); + + +/** + * @brief Set LCD background to custom RGB color value + * @param port Pointer to device structure for driver instance. + * @param r A numeric value for the red color (max is 255) + * @param g A numeric value for the green color (max is 255) + * @param b A numeric value for the blue color (max is 255) + */ +void glcd_color_set(struct device *port, uint8_t r, uint8_t g, uint8_t b); + + +/** + * @brief Initialize the Grove LCD panel + * @param port Pointer to device structure for driver instance. + * @return Returns DEV_OK if all passes + */ +int glcd_initialize(struct device *port); + +#endif /* DISPLAY_GROVE_LCD_H */