2 * SSD0303 OLED controller with OSRAM Pictiva 96x16 display.
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
10 /* The controller can support a variety of different displays, but we only
11 implement one. Most of the commends relating to brightness and geometry
15 //#define DEBUG_SSD0303 1
18 #define DPRINTF(fmt, args...) \
19 do { printf("ssd0303: " fmt , ##args); } while (0)
20 #define BADF(fmt, args...) \
21 do { fprintf(stderr, "ssd0303: error: " fmt , ##args); exit(1);} while (0)
23 #define DPRINTF(fmt, args...) do {} while(0)
24 #define BADF(fmt, args...) \
25 do { fprintf(stderr, "ssd0303: error: " fmt , ##args);} while (0)
28 /* Scaling factor for pixels. */
54 enum ssd0303_mode mode;
55 enum ssd0303_cmd cmd_state;
56 uint8_t framebuffer[132*8];
59 static int ssd0303_recv(i2c_slave *i2c)
61 BADF("Reads not implemented\n");
65 static int ssd0303_send(i2c_slave *i2c, uint8_t data)
67 ssd0303_state *s = (ssd0303_state *)i2c;
68 enum ssd0303_cmd old_cmd_state;
71 DPRINTF("byte 0x%02x\n", data);
73 s->mode = SSD0303_CMD;
74 else if (data == 0x40)
75 s->mode = SSD0303_DATA;
77 BADF("Unexpected byte 0x%x\n", data);
80 DPRINTF("data 0x%02x\n", data);
82 s->framebuffer[s->col + s->row * 132] = data;
88 old_cmd_state = s->cmd_state;
89 s->cmd_state = SSD0303_CMD_NONE;
90 switch (old_cmd_state) {
91 case SSD0303_CMD_NONE:
92 DPRINTF("cmd 0x%02x\n", data);
93 s->mode = SSD0303_IDLE;
95 case 0x00 ... 0x0f: /* Set lower colum address. */
96 s->col = (s->col & 0xf0) | (data & 0xf);
98 case 0x10 ... 0x20: /* Set higher column address. */
99 s->col = (s->col & 0x0f) | ((data & 0xf) << 4);
101 case 0x40 ... 0x7f: /* Set start line. */
104 case 0x81: /* Set contrast (Ignored). */
105 s->cmd_state = SSD0303_CMD_SKIP1;
107 case 0xa0: /* Mirror off. */
110 case 0xa1: /* Mirror off. */
113 case 0xa4: /* Entire display off. */
116 case 0xa5: /* Entire display on. */
119 case 0xa6: /* Inverse off. */
122 case 0xa7: /* Inverse on. */
125 case 0xa8: /* Set multipled ratio (Ignored). */
126 s->cmd_state = SSD0303_CMD_SKIP1;
128 case 0xad: /* DC-DC power control. */
129 s->cmd_state = SSD0303_CMD_SKIP1;
131 case 0xae: /* Display off. */
134 case 0xaf: /* Display on. */
137 case 0xb0 ... 0xbf: /* Set Page address. */
140 case 0xc0 ... 0xc8: /* Set COM output direction (Ignored). */
142 case 0xd3: /* Set display offset (Ignored). */
143 s->cmd_state = SSD0303_CMD_SKIP1;
145 case 0xd5: /* Set display clock (Ignored). */
146 s->cmd_state = SSD0303_CMD_SKIP1;
148 case 0xd8: /* Set color and power mode (Ignored). */
149 s->cmd_state = SSD0303_CMD_SKIP1;
151 case 0xd9: /* Set pre-charge period (Ignored). */
152 s->cmd_state = SSD0303_CMD_SKIP1;
154 case 0xda: /* Set COM pin configuration (Ignored). */
155 s->cmd_state = SSD0303_CMD_SKIP1;
157 case 0xdb: /* Set VCOM dselect level (Ignored). */
158 s->cmd_state = SSD0303_CMD_SKIP1;
160 case 0xe3: /* no-op. */
163 BADF("Unknown command: 0x%x\n", data);
166 case SSD0303_CMD_SKIP1:
167 DPRINTF("skip 0x%02x\n", data);
175 static void ssd0303_event(i2c_slave *i2c, enum i2c_event event)
177 ssd0303_state *s = (ssd0303_state *)i2c;
180 s->mode = SSD0303_IDLE;
190 static void ssd0303_update_display(void *opaque)
192 ssd0303_state *s = (ssd0303_state *)opaque;
199 char colortab[MAGNIFY * 8];
204 switch (s->ds->depth) {
220 BADF("Bad color depth\n");
223 dest_width *= MAGNIFY;
224 memset(colortab, 0xff, dest_width);
225 memset(colortab + dest_width, 0, dest_width);
227 colors[0] = colortab;
228 colors[1] = colortab;
229 } else if (s->inverse) {
230 colors[0] = colortab;
231 colors[1] = colortab + dest_width;
233 colors[0] = colortab + dest_width;
234 colors[1] = colortab;
237 for (y = 0; y < 16; y++) {
238 line = (y + s->start_line) & 63;
239 src = s->framebuffer + 132 * (line >> 3) + 36;
240 mask = 1 << (line & 7);
241 for (x = 0; x < 96; x++) {
242 memcpy(dest, colors[(*src & mask) != 0], dest_width);
246 for (x = 1; x < MAGNIFY; x++) {
247 memcpy(dest, dest - dest_width * 96, dest_width * 96);
248 dest += dest_width * 96;
252 dpy_update(s->ds, 0, 0, 96 * MAGNIFY, 16 * MAGNIFY);
255 static void ssd0303_invalidate_display(void * opaque)
257 ssd0303_state *s = (ssd0303_state *)opaque;
261 void ssd0303_init(DisplayState *ds, i2c_bus *bus, int address)
265 s = (ssd0303_state *)i2c_slave_init(bus, address, sizeof(ssd0303_state));
267 s->i2c.event = ssd0303_event;
268 s->i2c.recv = ssd0303_recv;
269 s->i2c.send = ssd0303_send;
270 graphic_console_init(ds, ssd0303_update_display, ssd0303_invalidate_display,
272 dpy_resize(s->ds, 96 * MAGNIFY, 16 * MAGNIFY);