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