4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
33 * \brief Himax HX8347 LCD controller driver
35 * \author Stefano Fedrigo <aleph@develer.com>
37 * Display initialization sequence is based on Atmel's softpack library
38 * implementation, see license below.
41 /* ----------------------------------------------------------------------------
42 * ATMEL Microcontroller Software Support
43 * ----------------------------------------------------------------------------
44 * Copyright (c) 2010, Atmel Corporation
46 * All rights reserved.
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions are met:
51 * - Redistributions of source code must retain the above copyright notice,
52 * this list of conditions and the disclaimer below.
54 * Atmel's name may not be used to endorse or promote products derived from
55 * this software without specific prior written permission.
57 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
59 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
60 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
62 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
63 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
64 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
65 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
66 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 * ----------------------------------------------------------------------------
70 #include "lcd_hx8347.h"
72 #include "hw/hw_hx8347.h"
73 #include <cfg/debug.h>
74 #include <drv/timer.h>
76 // Himax HX8347 chip id
77 #define HX8347_ID_HIMAX 0x47
80 static uint16_t lcd_row[LCD_WIDTH];
85 uint8_t cmd; // Register index, if 0xFF wait for value ms
86 uint8_t data; // Register value
89 static const struct lcd_hx8347_reg init_seq[] =
92 { 0x19, 0x49 }, // OSCADJ=10 0000, OSD_EN=1 //60Hz
93 { 0x93, 0x0C }, // RADJ=1100
96 { 0x44, 0x4D }, // VCM=100 1101
97 { 0x45, 0x11 }, // VDV=1 0001
98 { 0x20, 0x40 }, // BT=0100
99 { 0x1D, 0x07 }, // VC1=111
100 { 0x1E, 0x00 }, // VC3=000
101 { 0x1F, 0x04 }, // VRH=0100
103 { 0x1C, 0x04 }, // AP=100
104 { 0x1B, 0x10 }, // GASENB=0, PON=1, DK=0, XDK=0, DDVDH_TRI=0, STB=0
105 { 0xFF, 50 }, // 50 ms delay
107 { 0x43, 0x80 }, // Set VCOMG=1
108 { 0xFF, 50 }, // 50 ms delay
126 // 240x320 window setting
127 { 0x02, 0x00 }, // Column address start2
128 { 0x03, 0x00 }, // Column address start1
129 { 0x04, 0x00 }, // Column address end2
130 { 0x05, 0xEF }, // Column address end1
131 { 0x06, 0x00 }, // Row address start2
132 { 0x07, 0x00 }, // Row address start1
133 { 0x08, 0x01 }, // Row address end2
134 { 0x09, 0x3F }, // Row address end1
137 { 0x01, 0x06 }, // IDMON=0, INVON=1, NORON=1, PTLON=0
138 // { 0x16, 0xC8 }, // MY=1, MX=1, MV=0, BGR=1
139 { 0x16, 0x68 }, // MY=0, MX=1, MV=1, RGB XY exchange X mirror
140 { 0x23, 0x95 }, // N_DC=1001 0101
141 { 0x24, 0x95 }, // P_DC=1001 0101
142 { 0x25, 0xFF }, // I_DC=1111 1111
143 { 0x27, 0x06 }, // N_BP=0000 0110
144 { 0x28, 0x06 }, // N_FP=0000 0110
145 { 0x29, 0x06 }, // P_BP=0000 0110
146 { 0x2A, 0x06 }, // P_FP=0000 0110
147 { 0x2C, 0x06 }, // I_BP=0000 0110
148 { 0x2D, 0x06 }, // I_FP=0000 0110
149 { 0x3A, 0x01 }, // N_RTN=0000, N_NW=001
150 { 0x3B, 0x01 }, // P_RTN=0000, P_NW=001
151 { 0x3C, 0xF0 }, // I_RTN=1111, I_NW=000
152 { 0x3D, 0x00 }, // DIV=00
153 { 0x3E, 0x38 }, // SON=38h
154 { 0x40, 0x0F }, // GDON=0Fh
155 { 0x41, 0xF0 }, // GDOF=F0h
159 * Write to an LCD register.
161 static void regWrite(uint8_t reg, uint16_t val)
168 * Read data from a LCD register.
170 static uint16_t regRead(uint8_t reg)
173 return hx8347_read();
177 * Write data in a buffer to the LCD controller.
179 static void bufferWrite(const uint16_t *buf, uint16_t size)
182 for (i = 0 ; i < size; ++i)
183 hx8347_write(buf[i]);
186 static void lcd_setCursor(uint16_t x, uint16_t y)
188 regWrite(0x02, x >> 8);
189 regWrite(0x03, x & 0xff);
190 regWrite(0x06, y >> 8);
191 regWrite(0x07, y & 0xff);
194 static void lcd_setWindow(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
196 ASSERT(x + width <= LCD_WIDTH);
197 ASSERT(y + height <= LCD_HEIGHT);
201 // Window right and bottom limits are inclusive
207 regWrite(0x04, (x + width) >> 8);
208 regWrite(0x05, (x + width) & 0xff);
209 regWrite(0x08, (y + height) >> 8);
210 regWrite(0x09, (y + height) & 0xff);
214 * Refresh a bitmap on screen
216 void lcd_hx8347_blitBitmap(const Bitmap *bm)
221 lcd_setWindow(0, 0, bm->width, bm->height);
224 for (l = 0; l < bm->height / 8; l++)
226 for (mask = 1; mask; mask <<= 1)
228 for (i = 0; i < bm->width; i++)
230 if (bm->raster[l * bm->width + i] & mask)
235 bufferWrite(lcd_row, bm->width);
239 for (r = 0, mask = 1; r < bm->height % 8; r++, mask <<= 1)
241 for (i = 0; i < bm->width; i++)
243 if (bm->raster[l * bm->width + i] & mask)
248 bufferWrite(lcd_row, bm->width);
253 * Blit a 24 bit color raw raster directly on screen
255 void lcd_hx8347_blitBitmap24(int x, int y, int width, int height, const char *bmp)
259 lcd_setWindow(x, y, width, height);
262 for (l = 0; l < height; l++)
264 for (r = 0; r < width; r++)
267 (((uint16_t)bmp[0] << 8) & 0xF800) |
268 (((uint16_t)bmp[1] << 3) & 0x07E0) |
269 (((uint16_t)bmp[2] >> 3) & 0x001F);
272 bufferWrite(lcd_row, width);
279 void lcd_hx8347_off(void)
281 regWrite(0x90, 0); // SAP=0000 0000
282 regWrite(0x26, 0); // GON=0, DTE=0, D=00
288 void lcd_hx8347_on(void)
290 regWrite(0x90, 0x7F); // SAP=0111 1111
291 regWrite(0x26, 0x04); // GON=0, DTE=0, D=01
293 regWrite(0x26, 0x24); // GON=1, DTE=0, D=01
294 regWrite(0x26, 0x2C); // GON=1, DTE=0, D=11
296 regWrite(0x26, 0x3C); // GON=1, DTE=1, D=11
300 * Display initialization.
302 void lcd_hx8347_init(void)
311 if ((chip_id = regRead(0x67)) != HX8347_ID_HIMAX)
313 kprintf("HX8347 chip id read error or wrong id (0x%x), skipping initialization.\n", chip_id);
317 for (i = 0; i < countof(init_seq); i++)
319 if (init_seq[i].cmd != 0xFF)
320 regWrite(init_seq[i].cmd, init_seq[i].data);
322 timer_delay(init_seq[i].data);