aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gdisp/AlteraFramereader/gdisp_lld_alteraframereader.c
blob: 57856ee8a527afef2e20f9947029296e4dc88b3c (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
/*
 * 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 <system.h>
#include <io.h>
#include <string.h>
#include "gfx.h"

#if GFX_USE_GDISP

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

#ifndef FRAMEREADER_BASE
	#error "Framereader IP base address (FRAMEREADER_BASE) not set in board file."
#endif
#ifndef SCREEN_WIDTH
	#error "Screen width (SCREEN_WIDTH) not set in board file."
#endif
#ifndef SCREEN_HEIGHT
	#error "Screen height (SCREEN_HEIGHT) not set in board file."
#endif

typedef struct fbPriv {
	void* pixels;			// The pixel buffer
	gCoord linelen;		// The number of bytes per display line
	void* frame0;
	void* frame1;
} fbPriv;

/*===========================================================================*/
/* Driver local routines    .                                                */
/*===========================================================================*/

#define PIXIL_POS(g, x, y)      ((y) * ((fbPriv *)(g)->priv)->linelen + (x) * sizeof(LLDCOLOR_TYPE))
#define PIXEL_ADDR(g, pos)      ((LLDCOLOR_TYPE *)(((char *)((fbPriv *)(g)->priv)->pixels)+pos))
#define PRIV(g)                 ((fbPriv *)g->priv)

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

LLDSPEC gBool gdisp_lld_init(GDisplay* g)
{
	// Allocate the frame buffers
	PRIV(g)->frame0 = gfxAlloc(SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(LLDCOLOR_TYPE));
	PRIV(g)->frame1 = gfxAlloc(SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(LLDCOLOR_TYPE));
	if (!PRIV(g)->frame0 || !PRIV(g)->frame1) {
		gfxHalt("Couldn't allocate memory for framebuffer\r\n");
	}

	// Initialize the private structure
	g->g.Width = SCREEN_WIDTH;
	g->g.Height = SCREEN_HEIGHT;
	g->g.Backlight = 100;
	g->g.Contrast = 50;
	g->g.Orientation = GDISP_ROTATE_0;
	g->g.Powermode = gPowerOn;
	g->board = 0;
	PRIV(g)->linelen = g->g.Width * sizeof(LLDCOLOR_TYPE);	// bytes per line
	PRIV(g)->pixels = PRIV(g)->frame0;
	
	// Make sure the MSB is set so we bypass the data cache of the NIOS-II soft core
	PRIV(g)->pixels = (void*)((char*)PRIV(g)->pixels + 0x80000000);
	
	// Stop the framereader to allow for configuration
	IOWR(ALT_VIP_VFR_0_BASE, 0x00, 0x00); // stop for config

	// Frame 0
	IOWR(ALT_VIP_VFR_0_BASE, 0x04, (uint32_t)PRIV(g)->frame0);	
	IOWR(ALT_VIP_VFR_0_BASE, 0x05, SCREEN_WIDTH*SCREEN_HEIGHT/2);
	IOWR(ALT_VIP_VFR_0_BASE, 0x06, SCREEN_WIDTH*SCREEN_HEIGHT);	
	IOWR(ALT_VIP_VFR_0_BASE, 0x08, SCREEN_WIDTH);
	IOWR(ALT_VIP_VFR_0_BASE, 0x09, SCREEN_HEIGHT);
	IOWR(ALT_VIP_VFR_0_BASE, 0x0a, 0x00);
	
	// Frame 1
	IOWR(ALT_VIP_VFR_0_BASE, 0x0b, (uint32_t)PRIV(g)->frame1);	
	IOWR(ALT_VIP_VFR_0_BASE, 0x0c, SCREEN_WIDTH*SCREEN_HEIGHT/2);
	IOWR(ALT_VIP_VFR_0_BASE, 0x0d, SCREEN_WIDTH*SCREEN_HEIGHT);	
	IOWR(ALT_VIP_VFR_0_BASE, 0x0f, SCREEN_WIDTH);
	IOWR(ALT_VIP_VFR_0_BASE, 0x10, SCREEN_HEIGHT);
	IOWR(ALT_VIP_VFR_0_BASE, 0x11, 0x00);

	// Select frame0 (user draws to frame0 -> double buffering disabled by default)
	IOWR(ALT_VIP_VFR_0_BASE, 0x03, 0x00);

	// Start the framebuffer reader
	IOWR(ALT_VIP_VFR_0_BASE, 0x00, 0x01);

	return gTrue;
}

LLDSPEC void gdisp_lld_draw_pixel(GDisplay* g)
{
	unsigned	pos;

	#if GDISP_NEED_CONTROL
		switch(g->g.Orientation) {
		case GDISP_ROTATE_0:
		default:
			pos = PIXIL_POS(g, g->p.x, g->p.y);
			break;
		case GDISP_ROTATE_90:
			pos = PIXIL_POS(g, g->p.y, g->g.Width-g->p.x-1);
			break;
		case GDISP_ROTATE_180:
			pos = PIXIL_POS(g, g->g.Width-g->p.x-1, g->g.Height-g->p.y-1);
			break;
		case GDISP_ROTATE_270:
			pos = PIXIL_POS(g, g->g.Height-g->p.y-1, g->p.x);
			break;
		}
	#else
		pos = PIXIL_POS(g, g->p.x, g->p.y);
	#endif

		PIXEL_ADDR(g, pos)[0] = gdispColor2Native(g->p.color);
}

LLDSPEC	gColor gdisp_lld_get_pixel_color(GDisplay* g)
{
	unsigned		pos;
	LLDCOLOR_TYPE	color;

	#if GDISP_NEED_CONTROL
		switch(g->g.Orientation) {
		case GDISP_ROTATE_0:
		default:
			pos = PIXIL_POS(g, g->p.x, g->p.y);
			break;
		case GDISP_ROTATE_90:
			pos = PIXIL_POS(g, g->p.y, g->g.Width-g->p.x-1);
			break;
		case GDISP_ROTATE_180:
			pos = PIXIL_POS(g, g->g.Width-g->p.x-1, g->g.Height-g->p.y-1);
			break;
		case GDISP_ROTATE_270:
			pos = PIXIL_POS(g, g->g.Height-g->p.y-1, g->p.x);
			break;
		}
	#else
		pos = PIXIL_POS(g, g->p.x, g->p.y);
	#endif

	color = PIXEL_ADDR(g, pos)[0];
	return gdispNative2Color(color);
}

#if GDISP_NEED_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 gPowerOn: case gPowerSleep: case gPowerDeepSleep:
				board_power(g, (gPowermode)g->p.ptr);
				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:
				case GDISP_ROTATE_180:
					if (g->g.Orientation == GDISP_ROTATE_90 || g->g.Orientation == GDISP_ROTATE_270) {
						gCoord		tmp;

						tmp = g->g.Width;
						g->g.Width = g->g.Height;
						g->g.Height = tmp;
					}
					break;
				case GDISP_ROTATE_90:
				case GDISP_ROTATE_270:
					if (g->g.Orientation == GDISP_ROTATE_0 || g->g.Orientation == GDISP_ROTATE_180) {
						gCoord		tmp;

						tmp = g->g.Width;
						g->g.Width = g->g.Height;
						g->g.Height = tmp;
					}
					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;
			board_backlight(g, (unsigned)g->p.ptr);
			g->g.Backlight = (unsigned)g->p.ptr;
			return;

		case GDISP_CONTROL_CONTRAST:
			if ((unsigned)g->p.ptr > 100) g->p.ptr = (void *)100;
			board_contrast(g, (unsigned)g->p.ptr);
			g->g.Contrast = (unsigned)g->p.ptr;
			return;

		case GDISP_CONTROL_BUFFERS_SWAP:
			if (PRIV(g)->pixels == PRIV(g)->frame0) {
				PRIV(g)->pixels = PRIV(g)->frame1;
				IOWR(ALT_VIP_VFR_0_BASE, 0x03, 0x00);
			} else {
				PRIV(g)->pixels = PRIV(g)->frame0;
				IOWR(ALT_VIP_VFR_0_BASE, 0x03, 0x01);
			}
			return;
			
		case GDISP_CONTROL_BUFFERS_ENABLE:
			// Display frame0, draw to frame1
			PRIV(g)->pixels = PRIV(g)->frame1;
			IOWR(ALT_VIP_VFR_0_BASE, 0x03, 0x00);
			return;

		case GDISP_CONTROL_BUFFERS_DISABLE:
			// Display frame0, draw to frame0
			PRIV(g)->pixels = PRIV(g)->frame0;
			IOWR(ALT_VIP_VFR_0_BASE, 0x03, 0x00);
			return;
		}
	}
#endif

#if GDISP_HARDWARE_FILLS
	LLDSPEC void gdisp_lld_fill_area(GDisplay* g)
	{
		int i;
		unsigned int addr;
		int bytes_per_line;
		int bytes_per_pixel;
		uint8_t* line;
	
		// Calculate some values required for further calculations
		bytes_per_pixel = ((fbPriv*)g->priv)->linelen/g->g.Width; // must be 4
		bytes_per_line = ((fbPriv*)g->priv)->linelen;
	
		// Allocate line buffer
		line = gfxAlloc(bytes_per_pixel * g->p.cx);
	
		// Fill the line buffer with the solid color
	    for (i = 0; i < bytes_per_pixel * g->p.cx; i += 4)  {
			*((gColor*)(line + i)) = g->p.color;
		}
	  
		// Calculate the address of the first pixel of the rectangle (top left corner)
		addr = (int)((char *)((fbPriv *)g->priv)->pixels + (g->p.y * bytes_per_line) + (g->p.x * bytes_per_pixel));
	
		// Copy filled line buffer to create rectangle
		for (i = 0; i < g->p.cy; i++) {
			memcpy((void*)addr, line, bytes_per_pixel * g->p.cx);
			addr += bytes_per_line;
		}
	
		// Free the line buffer
		gfxFree(line);		
	}
#endif	// GDISP_HARDWARE_FILLS

#endif /* GFX_USE_GDISP */