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