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