Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / drv / lcd_hx8347.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Himax HX8347 LCD controller driver
34  *
35  * \author Stefano Fedrigo <aleph@develer.com>
36  *
37  * Display initialization sequence is based on Atmel's softpack library
38  * implementation, see license below.
39  */
40
41 /* ----------------------------------------------------------------------------
42  *         ATMEL Microcontroller Software Support
43  * ----------------------------------------------------------------------------
44  * Copyright (c) 2010, Atmel Corporation
45  *
46  * All rights reserved.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions are met:
50  *
51  * - Redistributions of source code must retain the above copyright notice,
52  * this list of conditions and the disclaimer below.
53  *
54  * Atmel's name may not be used to endorse or promote products derived from
55  * this software without specific prior written permission.
56  *
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  * ----------------------------------------------------------------------------
68  */
69
70 #include "lcd_hx8347.h"
71
72 #include "hw/hw_hx8347.h"
73 #include <cfg/debug.h>
74 #include <drv/timer.h>
75
76 // Himax HX8347 chip id
77 #define HX8347_ID_HIMAX  0x47
78
79
80 static uint16_t lcd_row[LCD_WIDTH];
81
82
83 struct lcd_hx8347_reg
84 {
85         uint8_t cmd;    // Register index, if 0xFF wait for value ms
86         uint8_t data;   // Register value
87 };
88
89 static const struct lcd_hx8347_reg init_seq[] =
90 {
91         // Start internal OSC
92         { 0x19, 0x49 },   // OSCADJ=10 0000, OSD_EN=1 //60Hz
93         { 0x93, 0x0C },   // RADJ=1100
94
95         // Power on flow
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
102
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
106
107         { 0x43, 0x80 },   // Set VCOMG=1
108         { 0xFF, 50   },   // 50 ms delay
109
110 #if 0
111         // Gamma for CMO 2.8
112         { 0x46, 0x95 },
113         { 0x47, 0x51 },
114         { 0x48, 0x00 },
115         { 0x49, 0x36 },
116         { 0x4A, 0x11 },
117         { 0x4B, 0x66 },
118         { 0x4C, 0x14 },
119         { 0x4D, 0x77 },
120         { 0x4E, 0x13 },
121         { 0x4F, 0x4C },
122         { 0x50, 0x46 },
123         { 0x51, 0x46 },
124 #endif
125
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
135
136         // Display Setting
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
156 };
157
158 /*
159  * Write to an LCD register.
160  */
161 static void regWrite(uint8_t reg, uint16_t val)
162 {
163         hx8347_cmd(reg);
164         hx8347_write(val);
165 }
166
167 /*
168  * Read data from a LCD register.
169  */
170 static uint16_t regRead(uint8_t reg)
171 {
172         hx8347_cmd(reg);
173         return hx8347_read();
174 }
175
176 /*
177  * Write data in a buffer to the LCD controller.
178  */
179 static void bufferWrite(const uint16_t *buf, uint16_t size)
180 {
181         uint16_t i;
182         for (i = 0 ; i < size; ++i)
183                 hx8347_write(buf[i]);
184 }
185
186 static void lcd_setCursor(uint16_t x, uint16_t y)
187 {
188         regWrite(0x02, x >> 8);
189         regWrite(0x03, x & 0xff);
190         regWrite(0x06, y >> 8);
191         regWrite(0x07, y & 0xff);
192 }
193
194 static void lcd_setWindow(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
195 {
196         ASSERT(x + width <= LCD_WIDTH);
197         ASSERT(y + height <= LCD_HEIGHT);
198         ASSERT(width > 0);
199         ASSERT(height > 0);
200
201         // Window right and bottom limits are inclusive
202         width--;
203         height--;
204
205         lcd_setCursor(x, y);
206
207         regWrite(0x04, (x + width) >> 8);
208         regWrite(0x05, (x + width) & 0xff);
209         regWrite(0x08, (y + height) >> 8);
210         regWrite(0x09, (y + height) & 0xff);
211 }
212
213 /*
214  * Refresh a bitmap on screen
215  */
216 void lcd_hx8347_blitBitmap(const Bitmap *bm)
217 {
218         uint8_t mask;
219         int i, l, r;
220
221         lcd_setWindow(0, 0, bm->width, bm->height);
222         hx8347_cmd(0x22);
223
224         for (l = 0; l < bm->height / 8; l++)
225         {
226                 for (mask = 1; mask; mask <<= 1)
227                 {
228                         for (i = 0; i < bm->width; i++)
229                         {
230                                 if (bm->raster[l * bm->width + i] & mask)
231                                         lcd_row[i] = 0x0000;
232                                 else
233                                         lcd_row[i] = 0xFFFF;
234                         }
235                         bufferWrite(lcd_row, bm->width);
236                 }
237         }
238
239         for (r = 0, mask = 1; r < bm->height % 8; r++, mask <<= 1)
240         {
241                 for (i = 0; i < bm->width; i++)
242                 {
243                         if (bm->raster[l * bm->width + i] & mask)
244                                 lcd_row[i] = 0x0000;
245                         else
246                                 lcd_row[i] = 0xFFFF;
247                 }
248                 bufferWrite(lcd_row, bm->width);
249         }
250 }
251
252 /*
253  * Blit a 24 bit color raw raster directly on screen
254  */
255 void lcd_hx8347_blitBitmap24(int x, int y, int width, int height, const char *bmp)
256 {
257         int l, r;
258
259         lcd_setWindow(x, y, width, height);
260         hx8347_cmd(0x22);
261
262         for (l = 0; l < height; l++)
263         {
264                 for (r = 0; r < width; r++)
265                 {
266                         lcd_row[r] =
267                                 (((uint16_t)bmp[0] << 8) & 0xF800) |
268                                 (((uint16_t)bmp[1] << 3) & 0x07E0) |
269                                 (((uint16_t)bmp[2] >> 3) & 0x001F);
270                         bmp += 3;
271                 }
272                 bufferWrite(lcd_row, width);
273         }
274 }
275
276 /**
277  * Turn off display.
278  */
279 void lcd_hx8347_off(void)
280 {
281         regWrite(0x90, 0);  // SAP=0000 0000
282         regWrite(0x26, 0);  // GON=0, DTE=0, D=00
283 }
284
285 /**
286  * Turn on display.
287  */
288 void lcd_hx8347_on(void)
289 {
290         regWrite(0x90, 0x7F);  // SAP=0111 1111
291         regWrite(0x26, 0x04);  // GON=0, DTE=0, D=01
292         timer_delay(100);
293         regWrite(0x26, 0x24);  // GON=1, DTE=0, D=01
294         regWrite(0x26, 0x2C);  // GON=1, DTE=0, D=11
295         timer_delay(100);
296         regWrite(0x26, 0x3C);  // GON=1, DTE=1, D=11
297 }
298
299 /**
300  * Display initialization.
301  */
302 void lcd_hx8347_init(void)
303 {
304         unsigned i;
305         uint16_t chip_id;
306
307         hx8347_busInit();
308         lcd_hx8347_off();
309
310         // Check chip id
311         if ((chip_id = regRead(0x67)) != HX8347_ID_HIMAX)
312         {
313                 kprintf("HX8347 chip id read error or wrong id (0x%x), skipping initialization.\n", chip_id);
314                 return;
315         }
316
317         for (i = 0; i < countof(init_seq); i++)
318         {
319                 if (init_seq[i].cmd != 0xFF)
320                         regWrite(init_seq[i].cmd, init_seq[i].data);
321                 else
322                         timer_delay(init_seq[i].data);
323         }
324
325         lcd_hx8347_on();
326 }