Release version 2.5.1.
[bertos.git] / 2.5 / bertos / drv / lcd_rit128x96.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 OLED-RIT-128x96 (P14201) graphic display driver
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include "lcd_rit128x96.h"
39
40 #include "hw/hw_rit128x96.h"
41
42 #include <cfg/debug.h>
43 #include <cfg/macros.h>
44
45
46 /*
47  * Hard-coded command initialization sequence.
48  *
49  * NOTE: the first byte is the size of the command.
50  */
51 static const uint8_t init_cmd[] =
52 {
53         /* Unlock commands */
54         3, 0xfd, 0x12, 0xe3,
55         /* Display off */
56         2, 0xae, 0xe3,
57         /* Icon off */
58         3, 0x94, 0, 0xe3,
59         /* Multiplex ratio */
60         3, 0xa8, 95, 0xe3,
61         /* Contrast */
62         3, 0x81, 0xb7, 0xe3,
63         /* Pre-charge current */
64         3, 0x82, 0x3f, 0xe3,
65         /* Display Re-map */
66         3, 0xa0, 0x52, 0xe3,
67         /* Display Start Line */
68         3, 0xa1, 0, 0xe3,
69         /* Display Offset */
70         3, 0xa2, 0x00, 0xe3,
71         /* Display Mode Normal */
72         2, 0xa4, 0xe3,
73         /* Phase Length */
74         3, 0xb1, 0x11, 0xe3,
75         /* Frame frequency */
76         3, 0xb2, 0x23, 0xe3,
77         /* Front Clock Divider */
78         3, 0xb3, 0xe2, 0xe3,
79         /* Set gray scale table */
80         17, 0xb8, 1, 2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 19, 22, 26, 30, 0xe3,
81         /* Second pre-charge period */
82         3, 0xbb, 0x01, 0xe3,
83         /* Pre-charge voltage */
84         3, 0xbc, 0x3f, 0xe3,
85         /* Display ON */
86         2, 0xaf, 0xe3,
87 };
88
89 /*
90  * Hard-coded command shutdown sequence.
91  */
92 static const uint8_t exit_cmd[] =
93 {
94         /* Display OFF */
95         0xae, 0xe3
96 };
97
98 /*
99  * Hard-coded horizontal increment command.
100  */
101 static const uint8_t horizontal_inc[] =
102 {
103         0xa0, 0x52
104 };
105
106 /**
107  * Write a sequence of data bytes to the LCD controller
108  */
109 static void lcd_dataWrite(const uint8_t *buf, size_t count)
110 {
111         while (count--)
112                 LCD_WRITE(*buf++);
113 }
114
115 /* Turn on the OLED display */
116 void rit128x96_on(void)
117 {
118         unsigned int i;
119
120         /* Loop through the SSD1329 controller initialization sequence */
121         LCD_SET_COMMAND();
122         for (i = 0; i < sizeof(init_cmd); i += init_cmd[i] + 1)
123                 lcd_dataWrite(init_cmd + i + 1, init_cmd[i] - 1);
124 }
125
126 /* Turn off the OLED display */
127 void rit128x96_off(void)
128 {
129         LCD_SET_COMMAND();
130         lcd_dataWrite(exit_cmd, sizeof(exit_cmd));
131 }
132
133 static void lcd_start_blit(uint8_t x, uint8_t y, uint8_t width, uint8_t height)
134 {
135         uint8_t buffer[3];
136
137         ASSERT((x + width) <= LCD_WIDTH);
138         ASSERT((y + height) <= LCD_HEIGHT);
139
140         /* Enter command mode */
141         LCD_SET_COMMAND();
142
143         buffer[0] = 0x15;
144         buffer[1] = x / 2;
145         buffer[2] = (x + width - 2) / 2;
146         lcd_dataWrite(buffer, 3);
147
148         buffer[0] = 0x75;
149         buffer[1] = y;
150         buffer[2] = y + height - 1;
151         lcd_dataWrite(buffer, 3);
152         lcd_dataWrite((const uint8_t *)&horizontal_inc, sizeof(horizontal_inc));
153 }
154
155 /* Refresh a raw image on screen */
156 void rit128x96_blitRaw(const uint8_t *data,
157                 uint8_t x, uint8_t y, uint8_t width, uint8_t height)
158 {
159         lcd_start_blit(x, y, width, height);
160         /*
161          * Enter data mode and send the encoded image data to the OLED display,
162          * over the SSI bus.
163          */
164         LCD_SET_DATA();
165         while (height--)
166         {
167                 /* Write an entire row at once */
168                 lcd_dataWrite(data, width / 2);
169                 data += width / 2;
170         }
171 }
172
173 /* Refresh a bitmap on screen */
174 void rit128x96_blitBitmap(const Bitmap *bm)
175 {
176         uint8_t lcd_row[bm->width / 2];
177         uint8_t mask;
178         int i, l;
179
180         lcd_start_blit(0, 0, bm->width, bm->height);
181         /*
182          * Enter data mode and send the encoded image data to the OLED display,
183          * over the SSI bus.
184          */
185         LCD_SET_DATA();
186         for (l = 0; l < bm->height / 8; l++)
187         {
188                 for (mask = 1; mask; mask <<= 1)
189                 {
190                         for (i = 0; i < bm->width; i++)
191                         {
192                                 if (bm->raster[l * bm->width + i] & mask)
193                                         lcd_row[i / 2] |= i & 1 ? 0x0f : 0xf0;
194                                 else
195                                         lcd_row[i / 2] &= i & 1 ? 0xf0 : 0x0f;
196                         }
197                         /* Write an entire row at once */
198                         lcd_dataWrite(lcd_row, sizeof(lcd_row));
199                 }
200         }
201 }
202
203 /* Initialize the OLED display */
204 void rit128x96_init(void)
205 {
206         /* Initialize the communication bus */
207         lcd_rit128x96_hw_bus_init();
208
209         /* Turn on the OLED display */
210         rit128x96_on();
211 }