LCD_Driver.c 9.19 KB
Newer Older
schneider's avatar
schneider committed
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
/*****************************************************************************
* | File      	:		LCD_Driver.c
* | Author      :   Waveshare team
* | Function    :   Electronic paper driver
* | Info        :
*----------------
* |	This version:   V1.0
* | Date        :   2018-11-18
* | Info        :   
#
# Permission is hereby granted, free of UBYTEge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to  whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#include "LCD_Driver.h"
31
32
#include "framebuffer.h"

schneider's avatar
schneider committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
static uint8_t screen[LCD_HEIGHT][LCD_WIDTH][2];

/*******************************************************************************
function:
	Hardware reset
*******************************************************************************/
static void LCD_Reset(void)
{
	//DEV_Digital_Write(DEV_CS_PIN,0);
	DEV_Delay_ms(20);
	DEV_RESET_LOW();
	DEV_Delay_ms(20);
	DEV_RESET_HIGH();
	DEV_Delay_ms(20);
}

/*******************************************************************************
function:
	Setting backlight
parameter	:
53
		value : Range 0~100   Duty cycle is value/100
schneider's avatar
schneider committed
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
*******************************************************************************/
void LCD_SetBacklight(UWORD Value)
{
	DEV_Set_BL(Value);
}

/*******************************************************************************
function:
		Write register address and data
*******************************************************************************/
void LCD_WriteData_Byte(UBYTE da)
{
	//DEV_Digital_Write(DEV_CS_PIN,0);
	DEV_Digital_Write(DEV_DC_PIN, 1);
	DEV_SPI_WRITE(da);
	//DEV_Digital_Write(DEV_CS_PIN,1);
}

void LCD_WriteData_Word(UWORD da)
{
	UBYTE i = (da >> 8) & 0xff;
	//DEV_Digital_Write(DEV_CS_PIN,0);
	DEV_Digital_Write(DEV_DC_PIN, 1);
	DEV_SPI_WRITE(i);
	uint8_t tmp = da;
	DEV_SPI_WRITE(tmp);
	//DEV_Digital_Write(DEV_CS_PIN,1);
}

void LCD_WriteReg(UBYTE da)
{
	//DEV_Digital_Write(DEV_CS_PIN,0);
	DEV_Digital_Write(DEV_DC_PIN, 0);
	DEV_SPI_WRITE(da);
	//DEV_Digital_Write(DEV_CS_PIN,1);
}

/******************************************************************************
function:	
		Common register initialization
******************************************************************************/
void LCD_Init(void)
{
	LCD_Reset();

	//************* Start Initial Sequence **********//
	LCD_WriteReg(0x11); //Sleep exit
	DEV_Delay_ms(120);
	LCD_WriteReg(0x21);
	LCD_WriteReg(0x21);

	LCD_WriteReg(0xB1);
	LCD_WriteData_Byte(0x05);
	LCD_WriteData_Byte(0x3A);
	LCD_WriteData_Byte(0x3A);

	LCD_WriteReg(0xB2);
	LCD_WriteData_Byte(0x05);
	LCD_WriteData_Byte(0x3A);
	LCD_WriteData_Byte(0x3A);

	LCD_WriteReg(0xB3);
	LCD_WriteData_Byte(0x05);
	LCD_WriteData_Byte(0x3A);
	LCD_WriteData_Byte(0x3A);
	LCD_WriteData_Byte(0x05);
	LCD_WriteData_Byte(0x3A);
	LCD_WriteData_Byte(0x3A);

	LCD_WriteReg(0xB4);
	LCD_WriteData_Byte(0x03);

	LCD_WriteReg(0xC0);
	LCD_WriteData_Byte(0x62);
	LCD_WriteData_Byte(0x02);
	LCD_WriteData_Byte(0x04);

	LCD_WriteReg(0xC1);
	LCD_WriteData_Byte(0xC0);

	LCD_WriteReg(0xC2);
	LCD_WriteData_Byte(0x0D);
	LCD_WriteData_Byte(0x00);

	LCD_WriteReg(0xC3);
	LCD_WriteData_Byte(0x8D);
	LCD_WriteData_Byte(0x6A);

	LCD_WriteReg(0xC4);
	LCD_WriteData_Byte(0x8D);
	LCD_WriteData_Byte(0xEE);

	LCD_WriteReg(0xC5); /*VCOM*/
	LCD_WriteData_Byte(0x0E);

	LCD_WriteReg(0xE0);
	LCD_WriteData_Byte(0x10);
	LCD_WriteData_Byte(0x0E);
	LCD_WriteData_Byte(0x02);
	LCD_WriteData_Byte(0x03);
	LCD_WriteData_Byte(0x0E);
	LCD_WriteData_Byte(0x07);
	LCD_WriteData_Byte(0x02);
	LCD_WriteData_Byte(0x07);
	LCD_WriteData_Byte(0x0A);
	LCD_WriteData_Byte(0x12);
	LCD_WriteData_Byte(0x27);
	LCD_WriteData_Byte(0x37);
	LCD_WriteData_Byte(0x00);
	LCD_WriteData_Byte(0x0D);
	LCD_WriteData_Byte(0x0E);
	LCD_WriteData_Byte(0x10);

	LCD_WriteReg(0xE1);
	LCD_WriteData_Byte(0x10);
	LCD_WriteData_Byte(0x0E);
	LCD_WriteData_Byte(0x03);
	LCD_WriteData_Byte(0x03);
	LCD_WriteData_Byte(0x0F);
	LCD_WriteData_Byte(0x06);
	LCD_WriteData_Byte(0x02);
	LCD_WriteData_Byte(0x08);
	LCD_WriteData_Byte(0x0A);
	LCD_WriteData_Byte(0x13);
	LCD_WriteData_Byte(0x26);
	LCD_WriteData_Byte(0x36);
	LCD_WriteData_Byte(0x00);
	LCD_WriteData_Byte(0x0D);
	LCD_WriteData_Byte(0x0E);
	LCD_WriteData_Byte(0x10);

	LCD_WriteReg(0x3A);
	LCD_WriteData_Byte(0x05);

	LCD_WriteReg(0x36);
	LCD_WriteData_Byte(0xA8); //

	LCD_WriteReg(0x29);
}

/******************************************************************************
function:	Set the cursor position
parameter	:
		Xstart: 	Start UWORD x coordinate
	  Ystart:		Start UWORD y coordinate
	  Xend  :		End UWORD coordinates
	  Yend  :		End UWORD coordinatesen
******************************************************************************/
void LCD_SetCursor(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
{
	Xstart = Xstart + 1;
	Xend   = Xend + 1;
	Ystart = Ystart + 26;
	Yend   = Yend + 26;
	LCD_WriteReg(0x2a);
	LCD_WriteData_Byte(Xstart >> 8);
	LCD_WriteData_Byte(Xstart);
	LCD_WriteData_Byte(Xend >> 8);
	LCD_WriteData_Byte(Xend);

	LCD_WriteReg(0x2b);
	LCD_WriteData_Byte(Ystart >> 8);
	LCD_WriteData_Byte(Ystart);
	LCD_WriteData_Byte(Yend >> 8);
	LCD_WriteData_Byte(Yend);

	LCD_WriteReg(0x2C);
}
#if 0
/******************************************************************************
function:	Clear screen function, refresh the screen to a certain color
parameter	:
		Color :		The color you want to clear all the screen
******************************************************************************/
void LCD_Clear(UWORD Color)
{
	UWORD i,j;  	
	LCD_SetCursor(0,0,LCD_WIDTH-1,LCD_HEIGHT-1);
	for(i = 0; i < LCD_WIDTH; i++){
		for(j = 0; j < LCD_HEIGHT; j++){
			LCD_WriteData_Word(Color);
		}
	 }
}
#endif

/******************************************************************************
function:	Refresh a certain area to the same color
parameter	:
		Xstart: 	Start UWORD x coordinate
	  Ystart:		Start UWORD y coordinate
	  Xend  :		End UWORD coordinates
	  Yend  :		End UWORD coordinates
	  color :		Set the color
******************************************************************************/
void LCD_ClearWindow(
	UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, UWORD color
) {
	UWORD i, j;
	LCD_SetCursor(Xstart, Ystart, Xend - 1, Yend - 1);
	for (i = Ystart; i <= Yend - 1; i++) {
		for (j = Xstart; j <= Xend - 1; j++) {
			LCD_WriteData_Word(color);
		}
	}
}

/******************************************************************************
function: Set the color of an area
parameter	:
		Xstart: 	Start UWORD x coordinate
	  Ystart:		Start UWORD y coordinate
	  Xend  :		End UWORD coordinates
	  Yend  :		End UWORD coordinates
	  Color :		Set the color
******************************************************************************/
void LCD_SetWindowColor(
	UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, UWORD Color
) {
	LCD_SetCursor(Xstart, Ystart, Xend, Yend);
	LCD_WriteData_Word(Color);
}

/******************************************************************************
function: Draw a UWORD
parameter	:
		X			: 	Set the X coordinate
	  Y			:		Set the Y coordinate
	  Color :		Set the color
******************************************************************************/
#if 0
void LCD_SetUWORD(UWORD x, UWORD y, UWORD Color)
{
	LCD_SetCursor(x,y,x,y);
	LCD_WriteData_Word(Color);
}
#endif

void LCD_SetUWORD(UWORD x, UWORD y, UWORD Color)
{
	screen[y][x][0] = (Color >> 8);
	screen[y][x][1] = (Color & 0xFF);
}

void LCD_Clear(UWORD Color)
{
	UWORD i, j;
	for (i = 0; i < LCD_WIDTH; i++) {
		for (j = 0; j < LCD_HEIGHT; j++) {
			LCD_SetUWORD(i, j, Color);
		}
	}
}

void LCD_Set(uint8_t *data, int len)
{
	LCD_SetCursor(0, 0, 160 - 1, 80 - 1);
	DEV_Digital_Write(DEV_DC_PIN, 1);
	lcd_write(data, len);
}

315
316
uint8_t *LCD_Framebuffer(void)
{
317
	return (uint8_t *)screen;
318
319
}

schneider's avatar
schneider committed
320
321
322
323
void LCD_Update(void)
{
	LCD_Set((uint8_t *)screen, sizeof(screen));
}
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358

static Color
lcd_fb_encode_color_rgb(struct framebuffer *fb, uint8_t r, uint8_t g, uint8_t b)
{
	r >>= (8 - 5);
	g >>= (8 - 6);
	b >>= (8 - 5);

	// RGB565
	Color o = 0;
	o |= (r << 11);
	o |= (g << 5);
	o |= b;
	return o;
}

static void lcd_fb_update(struct framebuffer *fb)
{
	LCD_Update();
}

static struct framebuffer framebuffer = { .data   = screen,
					  .width  = LCD_WIDTH,
					  .height = LCD_HEIGHT,
					  .stride = LCD_WIDTH * LCD_HEIGHT * 2,
					  .orientation = FB_O_180,

					  .encode_color_rgb =
						  lcd_fb_encode_color_rgb,
					  .update = lcd_fb_update };

struct framebuffer *LCD_framebuffer(void)
{
	return &framebuffer;
}