root / hw / ssd0303.c @ 4870852c
History | View | Annotate | Download (8.8 kB)
1 | 9ee6e8bb | pbrook | /*
|
---|---|---|---|
2 | 9ee6e8bb | pbrook | * SSD0303 OLED controller with OSRAM Pictiva 96x16 display.
|
3 | 9ee6e8bb | pbrook | *
|
4 | 9ee6e8bb | pbrook | * Copyright (c) 2006-2007 CodeSourcery.
|
5 | 9ee6e8bb | pbrook | * Written by Paul Brook
|
6 | 9ee6e8bb | pbrook | *
|
7 | 9ee6e8bb | pbrook | * This code is licenced under the GPL.
|
8 | 9ee6e8bb | pbrook | */
|
9 | 9ee6e8bb | pbrook | |
10 | 9ee6e8bb | pbrook | /* The controller can support a variety of different displays, but we only
|
11 | 9ee6e8bb | pbrook | implement one. Most of the commends relating to brightness and geometry
|
12 | 9ee6e8bb | pbrook | setup are ignored. */
|
13 | 87ecb68b | pbrook | #include "hw.h" |
14 | 87ecb68b | pbrook | #include "i2c.h" |
15 | 87ecb68b | pbrook | #include "console.h" |
16 | 9ee6e8bb | pbrook | |
17 | 9ee6e8bb | pbrook | //#define DEBUG_SSD0303 1
|
18 | 9ee6e8bb | pbrook | |
19 | 9ee6e8bb | pbrook | #ifdef DEBUG_SSD0303
|
20 | 9ee6e8bb | pbrook | #define DPRINTF(fmt, args...) \
|
21 | 9ee6e8bb | pbrook | do { printf("ssd0303: " fmt , ##args); } while (0) |
22 | 9ee6e8bb | pbrook | #define BADF(fmt, args...) \
|
23 | 9ee6e8bb | pbrook | do { fprintf(stderr, "ssd0303: error: " fmt , ##args); exit(1);} while (0) |
24 | 9ee6e8bb | pbrook | #else
|
25 | 9ee6e8bb | pbrook | #define DPRINTF(fmt, args...) do {} while(0) |
26 | 9ee6e8bb | pbrook | #define BADF(fmt, args...) \
|
27 | 9ee6e8bb | pbrook | do { fprintf(stderr, "ssd0303: error: " fmt , ##args);} while (0) |
28 | 9ee6e8bb | pbrook | #endif
|
29 | 9ee6e8bb | pbrook | |
30 | 9ee6e8bb | pbrook | /* Scaling factor for pixels. */
|
31 | 9ee6e8bb | pbrook | #define MAGNIFY 4 |
32 | 9ee6e8bb | pbrook | |
33 | 9ee6e8bb | pbrook | enum ssd0303_mode
|
34 | 9ee6e8bb | pbrook | { |
35 | 9ee6e8bb | pbrook | SSD0303_IDLE, |
36 | 9ee6e8bb | pbrook | SSD0303_DATA, |
37 | 9ee6e8bb | pbrook | SSD0303_CMD |
38 | 9ee6e8bb | pbrook | }; |
39 | 9ee6e8bb | pbrook | |
40 | 9ee6e8bb | pbrook | enum ssd0303_cmd {
|
41 | 9ee6e8bb | pbrook | SSD0303_CMD_NONE, |
42 | 9ee6e8bb | pbrook | SSD0303_CMD_SKIP1 |
43 | 9ee6e8bb | pbrook | }; |
44 | 9ee6e8bb | pbrook | |
45 | 9ee6e8bb | pbrook | typedef struct { |
46 | 9ee6e8bb | pbrook | i2c_slave i2c; |
47 | 9ee6e8bb | pbrook | DisplayState *ds; |
48 | 9ee6e8bb | pbrook | int row;
|
49 | 9ee6e8bb | pbrook | int col;
|
50 | 9ee6e8bb | pbrook | int start_line;
|
51 | 9ee6e8bb | pbrook | int mirror;
|
52 | 9ee6e8bb | pbrook | int flash;
|
53 | 9ee6e8bb | pbrook | int enabled;
|
54 | 9ee6e8bb | pbrook | int inverse;
|
55 | 9ee6e8bb | pbrook | int redraw;
|
56 | 9ee6e8bb | pbrook | enum ssd0303_mode mode;
|
57 | 9ee6e8bb | pbrook | enum ssd0303_cmd cmd_state;
|
58 | 9ee6e8bb | pbrook | uint8_t framebuffer[132*8]; |
59 | 9ee6e8bb | pbrook | } ssd0303_state; |
60 | 9ee6e8bb | pbrook | |
61 | 9ee6e8bb | pbrook | static int ssd0303_recv(i2c_slave *i2c) |
62 | 9ee6e8bb | pbrook | { |
63 | 9ee6e8bb | pbrook | BADF("Reads not implemented\n");
|
64 | 9ee6e8bb | pbrook | return -1; |
65 | 9ee6e8bb | pbrook | } |
66 | 9ee6e8bb | pbrook | |
67 | 9ee6e8bb | pbrook | static int ssd0303_send(i2c_slave *i2c, uint8_t data) |
68 | 9ee6e8bb | pbrook | { |
69 | 9ee6e8bb | pbrook | ssd0303_state *s = (ssd0303_state *)i2c; |
70 | 9ee6e8bb | pbrook | enum ssd0303_cmd old_cmd_state;
|
71 | 9ee6e8bb | pbrook | switch (s->mode) {
|
72 | 9ee6e8bb | pbrook | case SSD0303_IDLE:
|
73 | 9ee6e8bb | pbrook | DPRINTF("byte 0x%02x\n", data);
|
74 | 9ee6e8bb | pbrook | if (data == 0x80) |
75 | 9ee6e8bb | pbrook | s->mode = SSD0303_CMD; |
76 | 9ee6e8bb | pbrook | else if (data == 0x40) |
77 | 9ee6e8bb | pbrook | s->mode = SSD0303_DATA; |
78 | 9ee6e8bb | pbrook | else
|
79 | 9ee6e8bb | pbrook | BADF("Unexpected byte 0x%x\n", data);
|
80 | 9ee6e8bb | pbrook | break;
|
81 | 9ee6e8bb | pbrook | case SSD0303_DATA:
|
82 | 9ee6e8bb | pbrook | DPRINTF("data 0x%02x\n", data);
|
83 | 9ee6e8bb | pbrook | if (s->col < 132) { |
84 | 9ee6e8bb | pbrook | s->framebuffer[s->col + s->row * 132] = data;
|
85 | 9ee6e8bb | pbrook | s->col++; |
86 | 9ee6e8bb | pbrook | s->redraw = 1;
|
87 | 9ee6e8bb | pbrook | } |
88 | 9ee6e8bb | pbrook | break;
|
89 | 9ee6e8bb | pbrook | case SSD0303_CMD:
|
90 | 9ee6e8bb | pbrook | old_cmd_state = s->cmd_state; |
91 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_NONE; |
92 | 9ee6e8bb | pbrook | switch (old_cmd_state) {
|
93 | 9ee6e8bb | pbrook | case SSD0303_CMD_NONE:
|
94 | 9ee6e8bb | pbrook | DPRINTF("cmd 0x%02x\n", data);
|
95 | 9ee6e8bb | pbrook | s->mode = SSD0303_IDLE; |
96 | 9ee6e8bb | pbrook | switch (data) {
|
97 | 9ee6e8bb | pbrook | case 0x00 ... 0x0f: /* Set lower colum address. */ |
98 | 9ee6e8bb | pbrook | s->col = (s->col & 0xf0) | (data & 0xf); |
99 | 9ee6e8bb | pbrook | break;
|
100 | 9ee6e8bb | pbrook | case 0x10 ... 0x20: /* Set higher column address. */ |
101 | 9ee6e8bb | pbrook | s->col = (s->col & 0x0f) | ((data & 0xf) << 4); |
102 | 9ee6e8bb | pbrook | break;
|
103 | 9ee6e8bb | pbrook | case 0x40 ... 0x7f: /* Set start line. */ |
104 | 9ee6e8bb | pbrook | s->start_line = 0;
|
105 | 9ee6e8bb | pbrook | break;
|
106 | 9ee6e8bb | pbrook | case 0x81: /* Set contrast (Ignored). */ |
107 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_SKIP1; |
108 | 9ee6e8bb | pbrook | break;
|
109 | 9ee6e8bb | pbrook | case 0xa0: /* Mirror off. */ |
110 | 9ee6e8bb | pbrook | s->mirror = 0;
|
111 | 9ee6e8bb | pbrook | break;
|
112 | 9ee6e8bb | pbrook | case 0xa1: /* Mirror off. */ |
113 | 9ee6e8bb | pbrook | s->mirror = 1;
|
114 | 9ee6e8bb | pbrook | break;
|
115 | 9ee6e8bb | pbrook | case 0xa4: /* Entire display off. */ |
116 | 9ee6e8bb | pbrook | s->flash = 0;
|
117 | 9ee6e8bb | pbrook | break;
|
118 | 9ee6e8bb | pbrook | case 0xa5: /* Entire display on. */ |
119 | 9ee6e8bb | pbrook | s->flash = 1;
|
120 | 9ee6e8bb | pbrook | break;
|
121 | 9ee6e8bb | pbrook | case 0xa6: /* Inverse off. */ |
122 | 9ee6e8bb | pbrook | s->inverse = 0;
|
123 | 9ee6e8bb | pbrook | break;
|
124 | 9ee6e8bb | pbrook | case 0xa7: /* Inverse on. */ |
125 | 9ee6e8bb | pbrook | s->inverse = 1;
|
126 | 9ee6e8bb | pbrook | break;
|
127 | 9ee6e8bb | pbrook | case 0xa8: /* Set multipled ratio (Ignored). */ |
128 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_SKIP1; |
129 | 9ee6e8bb | pbrook | break;
|
130 | 9ee6e8bb | pbrook | case 0xad: /* DC-DC power control. */ |
131 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_SKIP1; |
132 | 9ee6e8bb | pbrook | break;
|
133 | 9ee6e8bb | pbrook | case 0xae: /* Display off. */ |
134 | 9ee6e8bb | pbrook | s->enabled = 0;
|
135 | 9ee6e8bb | pbrook | break;
|
136 | 9ee6e8bb | pbrook | case 0xaf: /* Display on. */ |
137 | 9ee6e8bb | pbrook | s->enabled = 1;
|
138 | 9ee6e8bb | pbrook | break;
|
139 | 9ee6e8bb | pbrook | case 0xb0 ... 0xbf: /* Set Page address. */ |
140 | 9ee6e8bb | pbrook | s->row = data & 7;
|
141 | 9ee6e8bb | pbrook | break;
|
142 | 9ee6e8bb | pbrook | case 0xc0 ... 0xc8: /* Set COM output direction (Ignored). */ |
143 | 9ee6e8bb | pbrook | break;
|
144 | 9ee6e8bb | pbrook | case 0xd3: /* Set display offset (Ignored). */ |
145 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_SKIP1; |
146 | 9ee6e8bb | pbrook | break;
|
147 | 9ee6e8bb | pbrook | case 0xd5: /* Set display clock (Ignored). */ |
148 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_SKIP1; |
149 | 9ee6e8bb | pbrook | break;
|
150 | 9ee6e8bb | pbrook | case 0xd8: /* Set color and power mode (Ignored). */ |
151 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_SKIP1; |
152 | 9ee6e8bb | pbrook | break;
|
153 | 9ee6e8bb | pbrook | case 0xd9: /* Set pre-charge period (Ignored). */ |
154 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_SKIP1; |
155 | 9ee6e8bb | pbrook | break;
|
156 | 9ee6e8bb | pbrook | case 0xda: /* Set COM pin configuration (Ignored). */ |
157 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_SKIP1; |
158 | 9ee6e8bb | pbrook | break;
|
159 | 9ee6e8bb | pbrook | case 0xdb: /* Set VCOM dselect level (Ignored). */ |
160 | 9ee6e8bb | pbrook | s->cmd_state = SSD0303_CMD_SKIP1; |
161 | 9ee6e8bb | pbrook | break;
|
162 | 9ee6e8bb | pbrook | case 0xe3: /* no-op. */ |
163 | 9ee6e8bb | pbrook | break;
|
164 | 9ee6e8bb | pbrook | default:
|
165 | 9ee6e8bb | pbrook | BADF("Unknown command: 0x%x\n", data);
|
166 | 9ee6e8bb | pbrook | } |
167 | 9ee6e8bb | pbrook | break;
|
168 | 9ee6e8bb | pbrook | case SSD0303_CMD_SKIP1:
|
169 | 9ee6e8bb | pbrook | DPRINTF("skip 0x%02x\n", data);
|
170 | 9ee6e8bb | pbrook | break;
|
171 | 9ee6e8bb | pbrook | } |
172 | 9ee6e8bb | pbrook | break;
|
173 | 9ee6e8bb | pbrook | } |
174 | 9ee6e8bb | pbrook | return 0; |
175 | 9ee6e8bb | pbrook | } |
176 | 9ee6e8bb | pbrook | |
177 | 9ee6e8bb | pbrook | static void ssd0303_event(i2c_slave *i2c, enum i2c_event event) |
178 | 9ee6e8bb | pbrook | { |
179 | 9ee6e8bb | pbrook | ssd0303_state *s = (ssd0303_state *)i2c; |
180 | 9ee6e8bb | pbrook | switch (event) {
|
181 | 9ee6e8bb | pbrook | case I2C_FINISH:
|
182 | 9ee6e8bb | pbrook | s->mode = SSD0303_IDLE; |
183 | 9ee6e8bb | pbrook | break;
|
184 | 9ee6e8bb | pbrook | case I2C_START_RECV:
|
185 | 9ee6e8bb | pbrook | case I2C_START_SEND:
|
186 | 9ee6e8bb | pbrook | case I2C_NACK:
|
187 | 9ee6e8bb | pbrook | /* Nothing to do. */
|
188 | 9ee6e8bb | pbrook | break;
|
189 | 9ee6e8bb | pbrook | } |
190 | 9ee6e8bb | pbrook | } |
191 | 9ee6e8bb | pbrook | |
192 | 9ee6e8bb | pbrook | static void ssd0303_update_display(void *opaque) |
193 | 9ee6e8bb | pbrook | { |
194 | 9ee6e8bb | pbrook | ssd0303_state *s = (ssd0303_state *)opaque; |
195 | 9ee6e8bb | pbrook | uint8_t *dest; |
196 | 9ee6e8bb | pbrook | uint8_t *src; |
197 | 9ee6e8bb | pbrook | int x;
|
198 | 9ee6e8bb | pbrook | int y;
|
199 | 9ee6e8bb | pbrook | int line;
|
200 | 9ee6e8bb | pbrook | char *colors[2]; |
201 | 9ee6e8bb | pbrook | char colortab[MAGNIFY * 8]; |
202 | 9ee6e8bb | pbrook | int dest_width;
|
203 | 9ee6e8bb | pbrook | uint8_t mask; |
204 | 9ee6e8bb | pbrook | |
205 | b115bb3f | pbrook | if (!s->redraw)
|
206 | b115bb3f | pbrook | return;
|
207 | b115bb3f | pbrook | |
208 | 0e1f5a0c | aliguori | switch (ds_get_bits_per_pixel(s->ds)) {
|
209 | b115bb3f | pbrook | case 0: |
210 | b115bb3f | pbrook | return;
|
211 | b115bb3f | pbrook | case 15: |
212 | b115bb3f | pbrook | dest_width = 2;
|
213 | b115bb3f | pbrook | break;
|
214 | b115bb3f | pbrook | case 16: |
215 | b115bb3f | pbrook | dest_width = 2;
|
216 | b115bb3f | pbrook | break;
|
217 | b115bb3f | pbrook | case 24: |
218 | b115bb3f | pbrook | dest_width = 3;
|
219 | b115bb3f | pbrook | break;
|
220 | b115bb3f | pbrook | case 32: |
221 | b115bb3f | pbrook | dest_width = 4;
|
222 | b115bb3f | pbrook | break;
|
223 | b115bb3f | pbrook | default:
|
224 | b115bb3f | pbrook | BADF("Bad color depth\n");
|
225 | b115bb3f | pbrook | return;
|
226 | b115bb3f | pbrook | } |
227 | b115bb3f | pbrook | dest_width *= MAGNIFY; |
228 | b115bb3f | pbrook | memset(colortab, 0xff, dest_width);
|
229 | b115bb3f | pbrook | memset(colortab + dest_width, 0, dest_width);
|
230 | b115bb3f | pbrook | if (s->flash) {
|
231 | b115bb3f | pbrook | colors[0] = colortab;
|
232 | b115bb3f | pbrook | colors[1] = colortab;
|
233 | b115bb3f | pbrook | } else if (s->inverse) { |
234 | b115bb3f | pbrook | colors[0] = colortab;
|
235 | b115bb3f | pbrook | colors[1] = colortab + dest_width;
|
236 | b115bb3f | pbrook | } else {
|
237 | b115bb3f | pbrook | colors[0] = colortab + dest_width;
|
238 | b115bb3f | pbrook | colors[1] = colortab;
|
239 | b115bb3f | pbrook | } |
240 | 0e1f5a0c | aliguori | dest = ds_get_data(s->ds); |
241 | b115bb3f | pbrook | for (y = 0; y < 16; y++) { |
242 | b115bb3f | pbrook | line = (y + s->start_line) & 63;
|
243 | b115bb3f | pbrook | src = s->framebuffer + 132 * (line >> 3) + 36; |
244 | b115bb3f | pbrook | mask = 1 << (line & 7); |
245 | b115bb3f | pbrook | for (x = 0; x < 96; x++) { |
246 | b115bb3f | pbrook | memcpy(dest, colors[(*src & mask) != 0], dest_width);
|
247 | b115bb3f | pbrook | dest += dest_width; |
248 | b115bb3f | pbrook | src++; |
249 | 9ee6e8bb | pbrook | } |
250 | b115bb3f | pbrook | for (x = 1; x < MAGNIFY; x++) { |
251 | b115bb3f | pbrook | memcpy(dest, dest - dest_width * 96, dest_width * 96); |
252 | b115bb3f | pbrook | dest += dest_width * 96;
|
253 | 9ee6e8bb | pbrook | } |
254 | 9ee6e8bb | pbrook | } |
255 | b115bb3f | pbrook | s->redraw = 0;
|
256 | 9ee6e8bb | pbrook | dpy_update(s->ds, 0, 0, 96 * MAGNIFY, 16 * MAGNIFY); |
257 | 9ee6e8bb | pbrook | } |
258 | 9ee6e8bb | pbrook | |
259 | 9ee6e8bb | pbrook | static void ssd0303_invalidate_display(void * opaque) |
260 | 9ee6e8bb | pbrook | { |
261 | 9ee6e8bb | pbrook | ssd0303_state *s = (ssd0303_state *)opaque; |
262 | 9ee6e8bb | pbrook | s->redraw = 1;
|
263 | 9ee6e8bb | pbrook | } |
264 | 9ee6e8bb | pbrook | |
265 | 23e39294 | pbrook | static void ssd0303_save(QEMUFile *f, void *opaque) |
266 | 23e39294 | pbrook | { |
267 | 23e39294 | pbrook | ssd0303_state *s = (ssd0303_state *)opaque; |
268 | 23e39294 | pbrook | |
269 | 23e39294 | pbrook | qemu_put_be32(f, s->row); |
270 | 23e39294 | pbrook | qemu_put_be32(f, s->col); |
271 | 23e39294 | pbrook | qemu_put_be32(f, s->start_line); |
272 | 23e39294 | pbrook | qemu_put_be32(f, s->mirror); |
273 | 23e39294 | pbrook | qemu_put_be32(f, s->flash); |
274 | 23e39294 | pbrook | qemu_put_be32(f, s->enabled); |
275 | 23e39294 | pbrook | qemu_put_be32(f, s->inverse); |
276 | 23e39294 | pbrook | qemu_put_be32(f, s->redraw); |
277 | 23e39294 | pbrook | qemu_put_be32(f, s->mode); |
278 | 23e39294 | pbrook | qemu_put_be32(f, s->cmd_state); |
279 | 23e39294 | pbrook | qemu_put_buffer(f, s->framebuffer, sizeof(s->framebuffer));
|
280 | 23e39294 | pbrook | |
281 | 23e39294 | pbrook | i2c_slave_save(f, &s->i2c); |
282 | 23e39294 | pbrook | } |
283 | 23e39294 | pbrook | |
284 | 23e39294 | pbrook | static int ssd0303_load(QEMUFile *f, void *opaque, int version_id) |
285 | 23e39294 | pbrook | { |
286 | 23e39294 | pbrook | ssd0303_state *s = (ssd0303_state *)opaque; |
287 | 23e39294 | pbrook | |
288 | 23e39294 | pbrook | if (version_id != 1) |
289 | 23e39294 | pbrook | return -EINVAL;
|
290 | 23e39294 | pbrook | |
291 | 23e39294 | pbrook | s->row = qemu_get_be32(f); |
292 | 23e39294 | pbrook | s->col = qemu_get_be32(f); |
293 | 23e39294 | pbrook | s->start_line = qemu_get_be32(f); |
294 | 23e39294 | pbrook | s->mirror = qemu_get_be32(f); |
295 | 23e39294 | pbrook | s->flash = qemu_get_be32(f); |
296 | 23e39294 | pbrook | s->enabled = qemu_get_be32(f); |
297 | 23e39294 | pbrook | s->inverse = qemu_get_be32(f); |
298 | 23e39294 | pbrook | s->redraw = qemu_get_be32(f); |
299 | 23e39294 | pbrook | s->mode = qemu_get_be32(f); |
300 | 23e39294 | pbrook | s->cmd_state = qemu_get_be32(f); |
301 | 23e39294 | pbrook | qemu_get_buffer(f, s->framebuffer, sizeof(s->framebuffer));
|
302 | 23e39294 | pbrook | |
303 | 23e39294 | pbrook | i2c_slave_load(f, &s->i2c); |
304 | 23e39294 | pbrook | |
305 | 23e39294 | pbrook | return 0; |
306 | 23e39294 | pbrook | } |
307 | 23e39294 | pbrook | |
308 | 3023f332 | aliguori | void ssd0303_init(i2c_bus *bus, int address) |
309 | 9ee6e8bb | pbrook | { |
310 | 9ee6e8bb | pbrook | ssd0303_state *s; |
311 | 9ee6e8bb | pbrook | |
312 | 9ee6e8bb | pbrook | s = (ssd0303_state *)i2c_slave_init(bus, address, sizeof(ssd0303_state));
|
313 | 9ee6e8bb | pbrook | s->i2c.event = ssd0303_event; |
314 | 9ee6e8bb | pbrook | s->i2c.recv = ssd0303_recv; |
315 | 9ee6e8bb | pbrook | s->i2c.send = ssd0303_send; |
316 | 3023f332 | aliguori | s->ds = graphic_console_init(ssd0303_update_display, |
317 | 3023f332 | aliguori | ssd0303_invalidate_display, |
318 | 3023f332 | aliguori | NULL, NULL, s); |
319 | 3023f332 | aliguori | qemu_console_resize(s->ds, 96 * MAGNIFY, 16 * MAGNIFY); |
320 | 23e39294 | pbrook | register_savevm("ssd0303_oled", -1, 1, ssd0303_save, ssd0303_load, s); |
321 | 9ee6e8bb | pbrook | } |