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