51428db6e595552a5411d2261e91762550a9eb74
[bertos.git] / bertos / drv / lcd_ili9225.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 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief ILI9225B 176x220 graphic 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_ili9225.h"
71
72 #include "hw/hw_ili9225.h"
73
74 #include <drv/timer.h>
75 #include <drv/ser.h>
76 #include <io/kfile.h>
77
78
79 static struct Serial *spi;
80
81
82 struct lcd_ili9225_reg
83 {
84         uint8_t  cmd;          // Register index, if 0xFF wait for value ms
85         uint16_t data;         // Register value
86 };
87
88 static const struct lcd_ili9225_reg init_seq[] =
89 {
90         {0x01, 0x011c},        // Set SS, SM, GS and NL bits
91         {0x02, 0x0100},        // Set 1 line inversion
92         {0x03, 0x1030},        // Entry Mode set GRAM write direction and BGR=1
93         {0x08, 0x0808},        // Set BP and FP
94         {0x0C, 0x0001},        // RGB Input Interface Control: 16-bit RGB interface
95         {0x0F, 0x0A01},        // Set frame rate: 83Hz
96         {0x20, LCD_WIDTH},     // Set GRAM Address
97         {0x21, LCD_HEIGHT},    // Set GRAM Address
98
99         /* power on sequence */
100         {0x10, 0x0A00},        // Set asp DSTB,STB
101         {0x11, 0x1038},        // SET APON PON AON VCI1EN VC
102         {0xFF, 50},            // Wait 50 ms
103
104         {0x12, 0x1121},        // Internal reference voltage = VCI
105         {0x13, 0x06CE},        // Set GVDD
106         {0x14, 0x676F},        // Set VCOMH/VCOML voltage
107
108         // Set gram area
109         {0x30, 0x0000},
110         {0x31, 0x00DB},
111         {0x32, 0x0000},
112         {0x33, 0x0000},
113         {0x34, 0x00DB},
114         {0x35, 0x0000},
115         {0x36, LCD_WIDTH},
116         {0x37, 0x0000},
117         {0x38, LCD_HEIGHT},
118         {0x39, 0x0000},
119
120         // Set gamma curve
121         {0x50, 0x0000},
122         {0x51, 0x060A},
123         {0x52, 0x0D0A},
124         {0x53, 0x0303},
125         {0x54, 0x0A0D},
126         {0x55, 0x0A06},
127         {0x56, 0x0000},
128         {0x57, 0x0303},
129         {0x58, 0x0000},
130         {0x59, 0x0000},
131 };
132
133 static void lcd_cmd(uint8_t cmd)
134 {
135         LCD_CS_LOW();
136         LCD_RS_LOW();
137         kfile_write(&spi->fd, &cmd, sizeof(cmd));
138 }
139
140 static void lcd_data(uint16_t data)
141 {
142         char bytes[2];
143         bytes[0] = data >> 8;
144         bytes[1] = data & 0xFF;
145
146         kfile_flush(&spi->fd);
147         LCD_RS_HIGH();
148         kfile_write(&spi->fd, bytes, 2);
149         kfile_flush(&spi->fd);
150         LCD_CS_HIGH();
151 }
152
153 static void lcd_regWrite(uint8_t reg, uint16_t data)
154 {
155         lcd_cmd(reg);
156         lcd_data(data);
157 }
158
159 static void lcd_startBlit(uint8_t x, uint8_t y, uint8_t width, uint8_t height)
160 {
161         ASSERT((x + width) <= LCD_WIDTH);
162         ASSERT((y + height) <= LCD_HEIGHT);
163
164         lcd_regWrite(0x36, x + width);
165         lcd_regWrite(0x37, x);
166         lcd_regWrite(0x38, y + height);
167         lcd_regWrite(0x39, y);
168
169         lcd_regWrite(0x20, x);
170         lcd_regWrite(0x21, y);
171 }
172
173 /*
174  * Refresh a raw image on screen
175  */
176 void lcd_ili9225_blitRaw(UNUSED_ARG(const uint8_t *, data),
177                 uint8_t x, uint8_t y, uint8_t width, uint8_t height)
178 {
179         lcd_startBlit(x, y, width, height);
180         // TODO
181 }
182
183 /*
184  * Refresh a bitmap on screen
185  */
186 void lcd_ili9225_blitBitmap(const Bitmap *bm)
187 {
188         //uint16_t lcd_row[bm->width];
189         uint8_t mask;
190         int i, l, r;
191
192         lcd_startBlit(0, 0, bm->width, bm->height);
193
194         for (l = 0; l < bm->height / 8; l++)
195         {
196                 for (mask = 1; mask; mask <<= 1)
197                 {
198                         for (i = 0; i < bm->width; i++)
199                         {
200                                 if (bm->raster[l * bm->width + i] & mask)
201                                         lcd_regWrite(0x22, 0xffff);
202                                 else
203                                         lcd_regWrite(0x22, 0x0000);
204                         }
205                 }
206         }
207
208         for (r = 0, mask = 1; r < bm->height % 8; r++, mask <<= 1)
209         {
210                 for (i = 0; i < bm->width; i++)
211                 {
212                         if (bm->raster[l * bm->width + i] & mask)
213                                 lcd_regWrite(0x22, 0xffff);
214                         else
215                                 lcd_regWrite(0x22, 0x0000);
216                 }
217         }
218 }
219
220 /**
221  * Turn off display.
222  */
223 void lcd_ili9225_off(void)
224 {
225         lcd_regWrite(0x07, 0x0000);
226 }
227
228 /**
229  * Turn on display.
230  */
231 void lcd_ili9225_on(void)
232 {
233         lcd_regWrite(0x07, 0x1017);
234 }
235
236 /**
237  * Reset display.
238  */
239 static void lcd_reset(void)
240 {
241         LCD_RESET_LOW();
242         timer_delay(20);
243         LCD_RESET_HIGH();
244         timer_delay(50);
245 }
246
247 /**
248  * Set display backlight intensity.
249  */
250 void lcd_ili9225_backlight(unsigned level)
251 {
252     unsigned i;
253
254     if (level > LCD_BACKLIGHT_MAX)
255             level = LCD_BACKLIGHT_MAX;
256
257     // Switch off backlight
258     LCD_BACKLIGHT_LOW();
259     timer_delay(1);
260
261     // Set new level
262     for (i = 0; i <= level; i++)
263     {
264             LCD_BACKLIGHT_LOW();
265             LCD_BACKLIGHT_LOW();
266             LCD_BACKLIGHT_LOW();
267
268             LCD_BACKLIGHT_HIGH();
269             LCD_BACKLIGHT_HIGH();
270             LCD_BACKLIGHT_HIGH();
271     }
272 }
273
274 /**
275  * Display initialization.
276  */
277 void lcd_ili9225_init(struct Serial *_spi)
278 {
279         unsigned i;
280
281         ASSERT(_spi);
282         spi = _spi;
283         lcd_ili9225_hw_bus_init();
284
285         lcd_reset();
286         lcd_ili9225_off();
287
288         for (i = 0; i < countof(init_seq); i++)
289         {
290                 if (init_seq[i].cmd != 0xFF)
291                         lcd_regWrite(init_seq[i].cmd, init_seq[i].data);
292                 else
293                         timer_delay(init_seq[i].data);
294         }
295
296         lcd_ili9225_on();
297 }