aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gdisp/SSD1331/gdisp_lld_SSD1331.c
blob: 5080aeef6d7a42099f7fa5b0343ae445eb913f32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.org/license.html
 */

#include "gfx.h"

#if GFX_USE_GDISP

#if defined(GDISP_SCREEN_HEIGHT) || defined(GDISP_SCREEN_HEIGHT)
	#if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
		#warning "GDISP: This low level driver does not support setting a screen size. It is being ignored."
	#elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
		COMPILER_WARNING("GDISP: This low level driver does not support setting a screen size. It is being ignored.")
	#endif
	#undef GDISP_SCREEN_WIDTH
	#undef GDISP_SCREEN_HEIGHT
#endif

#define GDISP_DRIVER_VMT			GDISPVMT_SSD1331
#include "gdisp_lld_config.h"
#include "../../../src/gdisp/gdisp_driver.h"

#include "board_SSD1331.h"

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

#ifndef GDISP_SCREEN_HEIGHT
	#define GDISP_SCREEN_HEIGHT		64
#endif
#ifndef GDISP_SCREEN_WIDTH
	#define GDISP_SCREEN_WIDTH		96
#endif
#ifndef GDISP_INITIAL_CONTRAST
	#define GDISP_INITIAL_CONTRAST	100
#endif
#ifndef GDISP_INITIAL_BACKLIGHT
	#define GDISP_INITIAL_BACKLIGHT	32
#endif

#include "SSD1331.h"

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

// Some common routines and macros
#define write_reg(g, reg, data)		{ write_cmd(g, reg); write_data(g, data); }

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

static const uint8_t initdata[] = {
	SSD1331_DISPLAY_OFF,
	SSD1331_START_LINE, 0x00,
	SSD1331_COM_OFFSET, 0x00,
	SSD1331_PIXELS_NORMAL,
	SSD1331_MULTIPLEX, 0x3F,
	SSD1331_RESET, SSD1331_RESET_OFF,
	SSD1331_POWER, SSD1331_POWER_ON,
	SSD1331_PHASE_PERIOD, 0x31,
	SSD1331_CLOCKS, 0xF0,
	SSD1331_PRECHARGE_A, 0x64,
	SSD1331_PRECHARGE_B, 0x78,
	SSD1331_PRECHARGE_C, 0x64,
	SSD1331_PRECHARGE_VOLTAGE, 0x3A,
	SSD1331_DESELECT_VOLTAGE, 0x3E,
	SSD1331_CONTRAST_A, 0x91,
	SSD1331_CONTRAST_B, 0x50,
	SSD1331_CONTRAST_C, 0x7D,
	SSD1331_BRIGHTNESS, (GDISP_INITIAL_BACKLIGHT*10)/63,
	#if GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB565
		SSD1331_MODE, SSD1331_MODE_16_BIT|SSD1331_MODE_COM_SPLIT,
	#elif GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_BGR565
		SSD1331_MODE, SSD1331_MODE_16_BIT|SSD1331_MODE_COM_SPLIT|SSD1331_MODE_BGR,
	#elif GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB332
		SSD1331_MODE, SSD1331_MODE_8_BIT|SSD1331_MODE_COM_SPLIT,
	#elif GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_BGR332
		SSD1331_MODE, SSD1331_MODE_8_BIT|SSD1331_MODE_COM_SPLIT|SSD1331_MODE_BGR,
	#else
		#error "SSD1331: Invalid color format"
	#endif
	SSD1331_DRAW_MODE, SSD1331_DRAW_FILLRECT,
};

LLDSPEC gBool gdisp_lld_init(GDisplay *g) {
	unsigned		i;

	// No private area for this controller
	g->priv = 0;

	// Initialise the board interface
	init_board(g);

	// Hardware reset
	setpin_reset(g, gTrue);
	gfxSleepMilliseconds(20);
	setpin_reset(g, gFalse);
	gfxSleepMilliseconds(20);

	// Get the bus for the following initialisation commands
	acquire_bus(g);

	for(i=0;i<sizeof(initdata);i++)
		write_cmd(g, initdata[i]);

	write_cmd(g, SSD1331_DISPLAY_ON);

    // Finish Init
    post_init_board(g);

 	// Release the bus
	release_bus(g);

	/* Initialise the GDISP structure */
	g->g.Width = GDISP_SCREEN_WIDTH;
	g->g.Height = GDISP_SCREEN_HEIGHT;
	g->g.Orientation = GDISP_ROTATE_0;
	g->g.Powermode = gPowerOn;
	g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
	g->g.Contrast = GDISP_INITIAL_CONTRAST;
	return gTrue;
}

#if GDISP_HARDWARE_STREAM_WRITE
	LLDSPEC	void gdisp_lld_write_start(GDisplay *g) {
		acquire_bus(g);
		write_cmd(g, SSD1331_COLUMN_ADDRESS);
		write_cmd(g, g->p.x);
		write_cmd(g, g->p.x + g->p.cx - 1);
		write_cmd(g, SSD1331_ROW_ADDRESS);
		write_cmd(g, g->p.y);
		write_cmd(g, g->p.y + g->p.cy - 1);
	}
	#if GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB565 || GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_BGR565
		LLDSPEC	void gdisp_lld_write_color(GDisplay *g) {
			LLDCOLOR_TYPE	c;

			c = gdispColor2Native(g->p.color);
			write_data(g, c >> 8);
			write_data(g, c);
		}
	#else
		LLDSPEC	void gdisp_lld_write_color(GDisplay *g) {
			write_data(g, gdispColor2Native(g->p.color));
		}
	#endif
	LLDSPEC	void gdisp_lld_write_stop(GDisplay *g) {
		release_bus(g);
	}
#endif

#if GDISP_HARDWARE_FILLS
	LLDSPEC void gdisp_lld_fill_area(GDisplay *g) {
		uint8_t		a6, b6, c6;

		acquire_bus(g);

		if (g->p.color == GFX_BLACK) {
			// Use clear window command
			write_cmd(g, SSD1331_DRAW_CLEAR);
			write_cmd(g, g->p.x);
			write_cmd(g, g->p.y);
			write_cmd(g, g->p.x + g->p.cx - 1);
			write_cmd(g, g->p.y + g->p.cy - 1);
			release_bus(g);
			return;
		}

		#if GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB565 || GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB332
			c6 = RED_OF(g->p.color) >> 2;
			#if COLOR_BITS_R < 5
				if (c6 & 0x20) c6 |= (1<<(6-COLOR_BITS_R))-1;
			#endif
			b6 = GREEN_OF(g->p.color) >> 2;
			#if COLOR_BITS_G < 6
				if (b6 & 0x20) b6 |= (1<<(6-COLOR_BITS_G))-1;
			#endif
			a6 = BLUE_OF(g->p.color) >> 2;
			#if COLOR_BITS_B < 5
				if (a6 & 0x20) a6 |= (1<<(6-COLOR_BITS_B))-1;
			#endif
		#else
			c6 = BLUE_OF(g->p.color) >> 2;
			#if COLOR_BITS_B < 5
				if (c6 & 0x20) c6 |= (1<<(6-COLOR_BITS_B))-1;
			#endif
			b6 = GREEN_OF(g->p.color) >> 2;
			#if COLOR_BITS_G < 6
				if (b6 & 0x20) b6 |= (1<<(6-COLOR_BITS_G))-1;
			#endif
			a6 = RED_OF(g->p.color) >> 2;
			#if COLOR_BITS_R < 5
				if (a6 & 0x20) a6 |= (1<<(6-COLOR_BITS_R))-1;
			#endif
		#endif
		if (g->p.cx == 1 || g->p.cy == 1) {
			// Use Line command
			write_cmd(g, SSD1331_DRAW_LINE);
			write_cmd(g, g->p.x);
			write_cmd(g, g->p.y);
			write_cmd(g, g->p.x + g->p.cx - 1);
			write_cmd(g, g->p.y + g->p.cy - 1);
		} else {
			// Use Rectangle command

			write_cmd(g, SSD1331_DRAW_RECT);
			write_cmd(g, g->p.x);
			write_cmd(g, g->p.y);
			write_cmd(g, g->p.x + g->p.cx - 1);
			write_cmd(g, g->p.y + g->p.cy - 1);
			//outline
			write_cmd(g, c6);
			write_cmd(g, b6);
			write_cmd(g, a6);
			//fill
		}

		write_cmd(g, c6);
		write_cmd(g, b6);
		write_cmd(g, a6);
		release_bus(g);
	}
#endif

#if GDISP_NEED_SCROLL && GDISP_HARDWARE_SCROLL
	LLDSPEC void gdisp_lld_vertical_scroll(GDisplay *g) {
		acquire_bus(g);
		write_cmd(g, SSD1331_DRAW_COPY);
		write_cmd(g, g->p.x);
		write_cmd(g, g->p.y);
		write_cmd(g, g->p.x + g->p.cx - 1);
		write_cmd(g, g->p.y + g->p.cy - 1);
		write_cmd(g, g->p.x);
		write_cmd(g, g->p.y - lines);
		release_bus(g);
	}
#endif

#if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
	LLDSPEC void gdisp_lld_control(GDisplay *g) {
		switch(g->p.x) {
		case GDISP_CONTROL_POWER:
			if (g->g.Powermode == (gPowermode)g->p.ptr)
				return;
			switch((gPowermode)g->p.ptr) {
			case gPowerOff:
			case gPowerSleep:
			case gPowerDeepSleep:
				acquire_bus(g);
				write_cmd(g, SSD1331_DISPLAY_OFF);
				release_bus(g);
				break;
			case gPowerOn:
				acquire_bus(g);
				write_cmd(g, SSD1331_DISPLAY_ON);
				release_bus(g);
				break;
			default:
				return;
			}
			g->g.Powermode = (gPowermode)g->p.ptr;
			return;

		/*
		case GDISP_CONTROL_ORIENTATION:
			if (g->g.Orientation == (orientation_t)g->p.ptr)
				return;
			switch((orientation_t)g->p.ptr) {
			case GDISP_ROTATE_0:
				acquire_bus(g);
				//TODO
				release_bus(g);
				g->g.Height = GDISP_SCREEN_HEIGHT;
				g->g.Width = GDISP_SCREEN_WIDTH;
				break;
			case GDISP_ROTATE_90:
				acquire_bus(g);
				//TODO
				release_bus(g);
				g->g.Height = GDISP_SCREEN_WIDTH;
				g->g.Width = GDISP_SCREEN_HEIGHT;
				break;
			case GDISP_ROTATE_180:
				acquire_bus(g);
				//TODO
				release_bus(g);
				g->g.Height = GDISP_SCREEN_HEIGHT;
				g->g.Width = GDISP_SCREEN_WIDTH;
				break;
			case GDISP_ROTATE_270:
				acquire_bus(g);
				//TODO
				release_bus(g);
				g->g.Height = GDISP_SCREEN_WIDTH;
				g->g.Width = GDISP_SCREEN_HEIGHT;
				break;
			default:
				return;
			}
			g->g.Orientation = (orientation_t)g->p.ptr;
			return;
		*/

        case GDISP_CONTROL_BACKLIGHT:
            if ((unsigned)g->p.ptr > 100)
            	g->p.ptr = (void *)100;
			acquire_bus(g);
            write_cmd(g, SSD1331_BRIGHTNESS);
            write_cmd(g, ((unsigned)g->p.ptr*10)/63);
			release_bus(g);
            g->g.Backlight = (unsigned)g->p.ptr;
            return;

		//case GDISP_CONTROL_CONTRAST:
        default:
            return;
		}
	}
#endif

#endif /* GFX_USE_GDISP */