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