aboutsummaryrefslogtreecommitdiffstats
path: root/src/display.c
blob: e9612999010ac5e906dd1fb6db5ebe1b92e8d0ea (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
/*
 * @file display.c
 * Display library for ILI9481 display
 *
 * Copyright (C) 2018 Clyne Sullivan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <display.h>

#include <clock.h>
#include <gpio.h>

#include <stdint.h>

#define LCD_RD  GPIO_PORT(A, 0)
#define LCD_WR  GPIO_PORT(A, 1)
#define LCD_RS  GPIO_PORT(A, 4)
#define LCD_CS  GPIO_PORT(B, 0)
#define LCD_RST GPIO_PORT(C, 1)
#define LCD_D0  GPIO_PORT(A, 9)
#define LCD_D1  GPIO_PORT(C, 7)
#define LCD_D2  GPIO_PORT(A, 10)
#define LCD_D3  GPIO_PORT(B, 3)
#define LCD_D4  GPIO_PORT(B, 5)
#define LCD_D5  GPIO_PORT(B, 4)
#define LCD_D6  GPIO_PORT(B, 10)
#define LCD_D7  GPIO_PORT(A, 8)

void dsp_dmode(int mode)
{
	static int old = 0;

	if (mode != old) {
		gpio_mode(LCD_D0, mode);
		gpio_mode(LCD_D1, mode);
		gpio_mode(LCD_D2, mode);
		gpio_mode(LCD_D3, mode);
		gpio_mode(LCD_D4, mode);
		gpio_mode(LCD_D5, mode);
		gpio_mode(LCD_D6, mode);
		gpio_mode(LCD_D7, mode);
		old = mode;
	}
}

// bbbbbggg gggrrrrr 
uint16_t dsp_color(uint8_t r, uint8_t g, uint8_t b)
{
	r &= 0x1F;
	g &= 0x3F;
	b &= 0x1F;
	uint16_t c = (b << 11) | (g << 5) | r;
	return c;
}

void dsp_write_data(uint8_t data)
{
	gpio_dout(LCD_D0, data & 0x01);
	gpio_dout(LCD_D1, data & 0x02);
	gpio_dout(LCD_D2, data & 0x04);
	gpio_dout(LCD_D3, data & 0x08);
	gpio_dout(LCD_D4, data & 0x10);
	gpio_dout(LCD_D5, data & 0x20);
	gpio_dout(LCD_D6, data & 0x40);
	gpio_dout(LCD_D7, data & 0x80);
	gpio_dout(LCD_WR, 0);
	gpio_dout(LCD_WR, 1);
}

uint8_t dsp_read_data(void)
{
	uint8_t ret = 0;
	gpio_dout(LCD_RD, 0);
	ret |= gpio_din(LCD_D0);
	ret |= gpio_din(LCD_D1) << 1;
	ret |= gpio_din(LCD_D2) << 2;
	ret |= gpio_din(LCD_D3) << 3;
	ret |= gpio_din(LCD_D4) << 4;
	ret |= gpio_din(LCD_D5) << 5;
	ret |= gpio_din(LCD_D6) << 6;
	ret |= gpio_din(LCD_D7) << 7;
	gpio_dout(LCD_RD, 1);
	return ret;
}

void dsp_write_cmd(uint8_t data)
{
	gpio_dout(LCD_RS, 0);
	dsp_write_data(data);
	gpio_dout(LCD_RS, 1);
}

void dsp_set_addr_base(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
	dsp_write_cmd(0x2A);
	dsp_write_data(x1 >> 8);
	dsp_write_data(x1 & 0xFF);
	dsp_write_data(x2 >> 8);
	dsp_write_data(x2 & 0xFF);
	dsp_write_cmd(0x2B);
	dsp_write_data(y1 >> 8);
	dsp_write_data(y1 & 0xFF);
	dsp_write_data(y2 >> 8);
	dsp_write_data(y2 & 0xFF);
}

void dsp_set_addr(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
	dsp_set_addr_base(x1, y1, x2, y2);
	dsp_write_cmd(0x2C); // begin writing
}

void dsp_set_addr_read(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
	dsp_set_addr_base(x1, y1, x2, y2);
	dsp_write_cmd(0x2E); // begin reading
}

void dsp_init(void)
{
	gpio_mode(LCD_CS, OUTPUT);
	gpio_mode(LCD_RS, OUTPUT);
	gpio_mode(LCD_RD, OUTPUT);
	gpio_mode(LCD_WR, OUTPUT);
	gpio_mode(LCD_RST, OUTPUT);
	dsp_dmode(OUTPUT);
	gpio_speed(LCD_CS, VERYHIGH);
	gpio_speed(LCD_RS, VERYHIGH);
	gpio_speed(LCD_RD, VERYHIGH);
	gpio_speed(LCD_WR, VERYHIGH);
	gpio_speed(LCD_RST, VERYHIGH);
	gpio_speed(LCD_D0, VERYHIGH);
	gpio_speed(LCD_D1, VERYHIGH);
	gpio_speed(LCD_D2, VERYHIGH);
	gpio_speed(LCD_D3, VERYHIGH);
	gpio_speed(LCD_D4, VERYHIGH);
	gpio_speed(LCD_D5, VERYHIGH);
	gpio_speed(LCD_D6, VERYHIGH);
	gpio_speed(LCD_D7, VERYHIGH);
	gpio_dout(LCD_CS, 0);
	gpio_dout(LCD_RS, 1);
	gpio_dout(LCD_RD, 1);
	gpio_dout(LCD_WR, 1);
	gpio_dout(LCD_RST, 1);
	gpio_dout(LCD_D0, 0);
	gpio_dout(LCD_D1, 0);
	gpio_dout(LCD_D2, 0);
	gpio_dout(LCD_D3, 0);
	gpio_dout(LCD_D4, 0);
	gpio_dout(LCD_D5, 0);
	gpio_dout(LCD_D6, 0);
	gpio_dout(LCD_D7, 0);

	delay(50);
	gpio_dout(LCD_RST, 0);
	delay(15);
	gpio_dout(LCD_RST, 1);
	delay(15);

	dsp_write_cmd(0x01); // soft reset
	delay(50);

	dsp_write_cmd(0xD0); // power setting
	dsp_write_data(0x07);
	dsp_write_data(0x42);
	dsp_write_data(0x18);
	dsp_write_cmd(0xD1); // vcom control
	dsp_write_data(0x00);
	dsp_write_data(0x07);
	dsp_write_data(0x10);
	dsp_write_cmd(0xD2); // power for normal mode
	dsp_write_data(0x01);
	dsp_write_data(0x02);
	dsp_write_cmd(0xD3);
	dsp_write_data(0x01);
	dsp_write_data(0x02);
	dsp_write_cmd(0xD4);
	dsp_write_data(0x01);
	dsp_write_data(0x02);
	dsp_write_cmd(0xC0); // panel driving setting
	dsp_write_data(0x10);
	dsp_write_data(0x3B);
	dsp_write_data(0x00);
	dsp_write_data(0x02);
	dsp_write_data(0x11);
	dsp_write_cmd(0xC5); // frame rate/inversion ctl
	dsp_write_data(0x03);

	dsp_write_cmd(0x36); // rot. and stuff
	dsp_write_data(0xA3);
	dsp_write_cmd(0x3A); // set pixel format
	dsp_write_data(0x55);
	dsp_write_cmd(0x11);
	delay(150);
	dsp_write_cmd(0x29); // set display on
	delay(500);
	/*dsp_write_cmd(0x33); // set scroll area
	dsp_write_data(0x00);
	dsp_write_data(0x00);
	dsp_write_data(LCD_HEIGHT >> 8);
	dsp_write_data((LCD_HEIGHT & 0xFF) - 1);
	dsp_write_data(0x00);
	dsp_write_data(0x00);
	dsp_write_cmd(0x37);
	dsp_write_data(0x00);
	dsp_write_data(0x00);*/
}