NanoPayload

Annotated stage2/lcd.c

217:44fcbb779a70
2017-06-28 Paul Boddie Replaced u8, u16 and u32 with uint8_t, uint16_t and uint32_t respectively.
paul@15 1
/*
paul@15 2
 * Ben NanoNote LCD initialisation, based on uboot-xburst and xburst-tools.
paul@15 3
 *
paul@33 4
 * Copyright (C) 2001-2002 Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
paul@217 5
 * Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
paul@15 6
 *
paul@63 7
 * This program is free software: you can redistribute it and/or modify
paul@63 8
 * it under the terms of the GNU General Public License as published by
paul@63 9
 * the Free Software Foundation, either version 3 of the License, or
paul@63 10
 * (at your option) any later version.
paul@15 11
 *
paul@63 12
 * This program is distributed in the hope that it will be useful,
paul@63 13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@63 14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@63 15
 * GNU General Public License for more details.
paul@15 16
 *
paul@63 17
 * You should have received a copy of the GNU General Public License
paul@63 18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
paul@15 19
 */
paul@15 20
paul@155 21
#include "board-display.h"
paul@34 22
paul@33 23
#include "jzlcd.h"
paul@33 24
#include "sdram.h"
paul@33 25
#include "board.h"
paul@15 26
paul@40 27
extern vidinfo_t panel_info;
paul@95 28
static unsigned long lcd_base;
paul@68 29
paul@100 30
static unsigned int get_line_length()
paul@100 31
{
paul@217 32
	return ALIGN((panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8, sizeof(uint32_t));
paul@100 33
}
paul@100 34
paul@217 35
static uint32_t *get_pixel32(unsigned short h, unsigned short v)
paul@68 36
{
paul@217 37
	return (uint32_t *) (lcd_base + v * get_line_length()) + h;
paul@100 38
}
paul@68 39
paul@217 40
static uint16_t *get_pixel16(unsigned short h, unsigned short v)
paul@100 41
{
paul@217 42
	return (uint16_t *) (lcd_base + v * get_line_length()) + h;
paul@100 43
}
paul@100 44
paul@217 45
static uint8_t *get_pixel8(unsigned short h, unsigned short v)
paul@101 46
{
paul@217 47
	return (uint8_t *) (lcd_base + v * get_line_length()) + h;
paul@101 48
}
paul@101 49
paul@217 50
static uint8_t *get_pixel4(unsigned short h, unsigned short v)
paul@103 51
{
paul@217 52
	return (uint8_t *) (lcd_base + v * get_line_length()) + h / 2;
paul@103 53
}
paul@103 54
paul@103 55
static inline unsigned short div(unsigned short num, unsigned short denom, unsigned short scale)
paul@103 56
{
paul@103 57
	return (scale * num) / denom;
paul@103 58
}
paul@103 59
paul@217 60
static uint32_t pixel(uint8_t r, uint8_t g, uint8_t b, uint8_t rmax, uint8_t gmax, uint8_t bmax, uint8_t rshift, uint8_t gshift, uint8_t bshift)
paul@104 61
{
paul@104 62
	return (div(r, 255, rmax) << rshift) | (div(g, 255, gmax) << gshift) | (div(b, 255, bmax) << bshift);
paul@104 63
}
paul@104 64
paul@217 65
uint32_t get_bitmap_value(unsigned short x, uint32_t value)
paul@162 66
{
paul@162 67
	return (value >> (((panel_info.vl_col - 1 - x) * 32) / panel_info.vl_col)) % 2 ? 0xffffffff : 0;
paul@162 68
}
paul@162 69
paul@217 70
static void get_colour(unsigned short h, unsigned short v, uint8_t rgb[], unsigned short pixel_type)
paul@100 71
{
paul@163 72
	unsigned short v_max = panel_info.vl_row - 1;
paul@163 73
	unsigned short h_max = panel_info.vl_col - 1;
paul@100 74
paul@138 75
	rgb[(pixel_type - 1) % 3] = div(h, h_max, 255);
paul@138 76
	rgb[pixel_type % 3] = div(v, v_max, 255);
paul@138 77
	rgb[(pixel_type + 1) % 3] = (rgb[0] + rgb[1]) / 2;
paul@101 78
}
paul@101 79
paul@217 80
static void set_pixel32(unsigned short h, unsigned short v, uint32_t value)
paul@161 81
{
paul@217 82
	uint32_t *pix = get_pixel32(h, v);
paul@161 83
	*pix = value;
paul@161 84
}
paul@161 85
paul@217 86
static void set_pixel16_565(unsigned short h, unsigned short v, uint32_t value)
paul@161 87
{
paul@217 88
	uint16_t *pix = get_pixel16(h, v);
paul@217 89
	*pix = (uint16_t) value;
paul@161 90
}
paul@161 91
paul@217 92
static void set_pixel8(unsigned short h, unsigned short v, uint32_t value)
paul@161 93
{
paul@217 94
	uint8_t *pix = get_pixel8(h, v);
paul@217 95
	*pix = (uint8_t) value;
paul@161 96
}
paul@161 97
paul@217 98
static void set_pixel4(unsigned short h, unsigned short v, uint32_t value)
paul@161 99
{
paul@217 100
	uint8_t *pix = get_pixel4(h, v);
paul@217 101
	uint8_t mask = h & 1 ? 0xf0 : 0x0f;
paul@217 102
	*pix = (*pix & mask) | ((uint8_t) value);
paul@161 103
}
paul@161 104
paul@217 105
void set_pixel(unsigned short h, unsigned short v, uint32_t value)
paul@161 106
{
paul@161 107
	switch (panel_info.vl_bpix)
paul@161 108
	{
paul@161 109
		case LCD_COLOR32:
paul@161 110
		set_pixel32(h, v, value);
paul@161 111
		break;
paul@161 112
paul@161 113
		case LCD_COLOR8:
paul@161 114
		set_pixel8(h, v, value);
paul@161 115
		break;
paul@161 116
paul@161 117
		case LCD_COLOR4:
paul@161 118
		set_pixel4(h, v, value);
paul@161 119
		break;
paul@161 120
paul@161 121
		case LCD_COLOR16:
paul@161 122
		default:
paul@161 123
		set_pixel16_565(h, v, value);
paul@161 124
		break;
paul@161 125
	}
paul@161 126
}
paul@138 127
static void test_pixel32(unsigned short h, unsigned short v, unsigned short pixel_type)
paul@101 128
{
paul@217 129
	uint8_t rgb[3];
paul@104 130
paul@138 131
	get_colour(h, v, rgb, pixel_type);
paul@162 132
	set_pixel32(h, v, pixel(rgb[0], rgb[1], rgb[2], 255, 255, 255, 16, 8, 0) | get_bitmap_value(h, pixel_type));
paul@100 133
}
paul@100 134
paul@138 135
static void test_pixel16_565(unsigned short h, unsigned short v, unsigned short pixel_type)
paul@100 136
{
paul@217 137
	uint8_t rgb[3];
paul@104 138
paul@138 139
	get_colour(h, v, rgb, pixel_type);
paul@162 140
	set_pixel16_565(h, v, pixel(rgb[0], rgb[1], rgb[2], 31, 63, 31, 11, 5, 0) | get_bitmap_value(h, pixel_type));
paul@101 141
}
paul@100 142
paul@138 143
static void test_pixel8(unsigned short h, unsigned short v, unsigned short pixel_type)
paul@101 144
{
paul@217 145
	uint8_t rgb[3];
paul@104 146
paul@138 147
	get_colour(h, v, rgb, pixel_type);
paul@162 148
	set_pixel8(h, v, pixel(rgb[0], rgb[1], rgb[2], 7, 7, 3, 5, 2, 0) | get_bitmap_value(h, pixel_type));
paul@100 149
}
paul@100 150
paul@138 151
static void test_pixel4(unsigned short h, unsigned short v, unsigned short pixel_type)
paul@103 152
{
paul@217 153
	uint8_t rgb[3];
paul@104 154
paul@138 155
	get_colour(h, v, rgb, pixel_type);
paul@162 156
	set_pixel4(h, v, pixel(rgb[0], rgb[1], rgb[2], 1, 2, 1, 3, 1, 0) | get_bitmap_value(h, pixel_type));
paul@103 157
}
paul@103 158
paul@138 159
void test_pixel(unsigned short h, unsigned short v, unsigned short pixel_type)
paul@100 160
{
paul@100 161
	switch (panel_info.vl_bpix)
paul@100 162
	{
paul@100 163
		case LCD_COLOR32:
paul@138 164
		test_pixel32(h, v, pixel_type);
paul@100 165
		break;
paul@100 166
paul@101 167
		case LCD_COLOR8:
paul@138 168
		test_pixel8(h, v, pixel_type);
paul@101 169
		break;
paul@101 170
paul@103 171
		case LCD_COLOR4:
paul@138 172
		test_pixel4(h, v, pixel_type);
paul@103 173
		break;
paul@103 174
paul@101 175
		case LCD_COLOR16:
paul@100 176
		default:
paul@138 177
		test_pixel16_565(h, v, pixel_type);
paul@100 178
		break;
paul@100 179
	}
paul@100 180
}
paul@100 181
paul@100 182
void clear_pixel32(unsigned short h, unsigned short v)
paul@100 183
{
paul@217 184
	uint32_t *pix = get_pixel32(h, v);
paul@100 185
	*pix = 0;
paul@100 186
}
paul@100 187
paul@100 188
void clear_pixel16(unsigned short h, unsigned short v)
paul@100 189
{
paul@217 190
	uint16_t *pix = get_pixel16(h, v);
paul@100 191
	*pix = 0;
paul@68 192
}
paul@68 193
paul@101 194
void clear_pixel8(unsigned short h, unsigned short v)
paul@101 195
{
paul@217 196
	uint8_t *pix = get_pixel8(h, v);
paul@101 197
	*pix = 0;
paul@101 198
}
paul@101 199
paul@103 200
void clear_pixel4(unsigned short h, unsigned short v)
paul@103 201
{
paul@217 202
	uint8_t *pix = get_pixel4(h, v);
paul@217 203
	uint8_t mask = h & 1 ? 0xf0 : 0x0f;
paul@103 204
	*pix = *pix & mask;
paul@103 205
}
paul@103 206
paul@68 207
void clear_pixel(unsigned short h, unsigned short v)
paul@68 208
{
paul@100 209
	switch (panel_info.vl_bpix)
paul@100 210
	{
paul@100 211
		case LCD_COLOR32:
paul@100 212
		clear_pixel32(h, v);
paul@100 213
		break;
paul@68 214
paul@101 215
		case LCD_COLOR8:
paul@101 216
		clear_pixel8(h, v);
paul@101 217
		break;
paul@101 218
paul@103 219
		case LCD_COLOR4:
paul@103 220
		clear_pixel4(h, v);
paul@103 221
		break;
paul@103 222
paul@101 223
		case LCD_COLOR16:
paul@100 224
		default:
paul@100 225
		clear_pixel16(h, v);
paul@100 226
		break;
paul@100 227
	}
paul@68 228
}
paul@40 229
paul@100 230
void test_pattern()
paul@15 231
{
paul@15 232
	unsigned short v_max  = panel_info.vl_row;
paul@15 233
	unsigned short h_max  = panel_info.vl_col;
paul@15 234
	unsigned short v, h;
paul@68 235
paul@68 236
	for (v = 0; v < v_max; v += 1) {
paul@68 237
		for (h = 0; h < h_max; h += 1) {
paul@138 238
			test_pixel(h, v, 1);
paul@68 239
		}
paul@68 240
	}
paul@68 241
}
paul@68 242
paul@97 243
void lcd_clear(unsigned long lcd_base)
paul@68 244
{
paul@68 245
	unsigned short v_max  = panel_info.vl_row;
paul@68 246
	unsigned short h_max  = panel_info.vl_col;
paul@68 247
	unsigned short v, h;
paul@95 248
	unsigned long *pix = (unsigned long *)lcd_base;
paul@15 249
paul@22 250
	for (v = 0; v < v_max; v += 1) {
paul@22 251
		for (h = 0; h < h_max; h += 1) {
paul@68 252
			*pix++ = 0;
paul@15 253
		}
paul@15 254
	}
paul@15 255
}
paul@15 256
paul@15 257
/* LCD initialisation. */
paul@15 258
paul@195 259
void lcd_init()
paul@15 260
{
paul@20 261
	__lcd_display_pin_init();
paul@20 262
	__lcd_display_on();
paul@20 263
paul@97 264
	lcd_base = lcd_ctrl_init();
paul@15 265
	lcd_clear(lcd_base);
paul@15 266
	lcd_enable();
paul@15 267
}