-
Notifications
You must be signed in to change notification settings - Fork 0
/
hx8347g.c
473 lines (402 loc) · 12 KB
/
hx8347g.c
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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/**
******************************************************************************
* @file hx8347g.c
* @author MCD Application Team
* @brief This file includes the LCD driver for HX8347G LCD.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "hx8347g.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup Components
* @{
*/
/** @addtogroup HX8347G
* @brief This file provides a set of functions needed to drive the
* HX8347G LCD.
* @{
*/
/** @defgroup HX8347G_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup HX8347G_Private_Defines
* @{
*/
/**
* @}
*/
/** @defgroup HX8347G_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup HX8347G_Private_Variables
* @{
*/
LCD_DrvTypeDef hx8347g_drv =
{
hx8347g_Init,
hx8347g_ReadID,
hx8347g_DisplayOn,
hx8347g_DisplayOff,
hx8347g_SetCursor,
hx8347g_WritePixel,
hx8347g_ReadPixel,
hx8347g_SetDisplayWindow,
hx8347g_DrawHLine,
hx8347g_DrawVLine,
hx8347g_GetLcdPixelWidth,
hx8347g_GetLcdPixelHeight,
hx8347g_DrawBitmap,
};
static uint8_t Is_hx8347g_Initialized = 0;
static uint16_t ArrayRGB[320] = {0};
/**
* @}
*/
/** @defgroup HX8347G_Private_FunctionPrototypes
* @{
*/
/**
* @}
*/
/** @defgroup HX8347G_Private_Functions
* @{
*/
/**
* @brief Initialise the HX8347G LCD Component.
* @param None
* @retval None
*/
void hx8347g_Init(void)
{
if(Is_hx8347g_Initialized == 0)
{
Is_hx8347g_Initialized = 1;
/* Initialise HX8347G low level bus layer --------------------------------*/
LCD_IO_Init();
/* HX8347G requests 120ms (worst case) after reset */
LCD_Delay(120);
/* Driving ability setting */
hx8347g_WriteReg(LCD_REG_234, 0x00);
hx8347g_WriteReg(LCD_REG_235, 0x20);
hx8347g_WriteReg(LCD_REG_236, 0x0C);
hx8347g_WriteReg(LCD_REG_237, 0xC4);
hx8347g_WriteReg(LCD_REG_232, 0x40);
hx8347g_WriteReg(LCD_REG_233, 0x38);
hx8347g_WriteReg(LCD_REG_241, 0x01);
hx8347g_WriteReg(LCD_REG_242, 0x10);
hx8347g_WriteReg(LCD_REG_39, 0xA3);
/* Adjust the Gamma Curve */
hx8347g_WriteReg(LCD_REG_64, 0x01);
hx8347g_WriteReg(LCD_REG_65, 0x00);
hx8347g_WriteReg(LCD_REG_66, 0x00);
hx8347g_WriteReg(LCD_REG_67, 0x10);
hx8347g_WriteReg(LCD_REG_68, 0x0E);
hx8347g_WriteReg(LCD_REG_69, 0x24);
hx8347g_WriteReg(LCD_REG_70, 0x04);
hx8347g_WriteReg(LCD_REG_71, 0x50);
hx8347g_WriteReg(LCD_REG_72, 0x02);
hx8347g_WriteReg(LCD_REG_73, 0x13);
hx8347g_WriteReg(LCD_REG_74, 0x19);
hx8347g_WriteReg(LCD_REG_75, 0x19);
hx8347g_WriteReg(LCD_REG_76, 0x16);
hx8347g_WriteReg(LCD_REG_80, 0x1B);
hx8347g_WriteReg(LCD_REG_81, 0x31);
hx8347g_WriteReg(LCD_REG_82, 0x2F);
hx8347g_WriteReg(LCD_REG_83, 0x3F);
hx8347g_WriteReg(LCD_REG_84, 0x3F);
hx8347g_WriteReg(LCD_REG_85, 0x3E);
hx8347g_WriteReg(LCD_REG_86, 0x2F);
hx8347g_WriteReg(LCD_REG_87, 0x7B);
hx8347g_WriteReg(LCD_REG_88, 0x09);
hx8347g_WriteReg(LCD_REG_89, 0x06);
hx8347g_WriteReg(LCD_REG_90, 0x06);
hx8347g_WriteReg(LCD_REG_91, 0x0C);
hx8347g_WriteReg(LCD_REG_92, 0x1D);
hx8347g_WriteReg(LCD_REG_93, 0xCC);
/* Power voltage setting */
hx8347g_WriteReg(LCD_REG_27, 0x1B);
hx8347g_WriteReg(LCD_REG_26, 0x01);
hx8347g_WriteReg(LCD_REG_36, 0x2F);
hx8347g_WriteReg(LCD_REG_37, 0x57);
/*****VCOM offset ****/
hx8347g_WriteReg(LCD_REG_35, 0x86);
hx8347g_DisplayOn();
/* Set GRAM Area - Partial Display Control */
hx8347g_WriteReg(LCD_REG_1, 0x00); /* DP_STB = 0, DP_STB_S = 0, SCROLL = 0, */
hx8347g_SetDisplayWindow(0, 0, hx8347g_GetLcdPixelWidth(), hx8347g_GetLcdPixelHeight());
hx8347g_WriteReg(LCD_REG_22, 0xA0); /* Memory access control: MY = 1, MX = 0, MV = 1, ML = 0 */
}
/* Set the Cursor */
hx8347g_SetCursor(0, 0);
/* Prepare to write GRAM */
LCD_IO_WriteReg(LCD_REG_34);
}
/**
* @brief Enables the Display.
* @param None
* @retval None
*/
void hx8347g_DisplayOn(void)
{
/* Power On sequence ---------------------------------------------------------*/
hx8347g_WriteReg(LCD_REG_24, 0x36); /* Display frame rate = 70Hz RADJ = '0110' */
hx8347g_WriteReg(LCD_REG_25, 0x01); /* OSC_EN = 1 */
hx8347g_WriteReg(LCD_REG_28, 0x06); /* AP[2:0] = 111 */
hx8347g_WriteReg(LCD_REG_31, 0x90); /* GAS=1, VOMG=00, PON=1, DK=0, XDK=0, DVDH_TRI=0, STB=0*/
LCD_Delay(10);
/* 262k/65k color selection */
hx8347g_WriteReg(LCD_REG_23, 0x05); /* default 0x06 262k color, 0x05 65k color */
/* SET PANEL */
hx8347g_WriteReg(LCD_REG_54, 0x09); /* SS_PANEL = 1, GS_PANEL = 0,REV_PANEL = 0, BGR_PANEL = 1 */
/* Display On */
hx8347g_WriteReg(LCD_REG_40, 0x38);
LCD_Delay(60);
hx8347g_WriteReg(LCD_REG_40, 0x3C);
}
/**
* @brief Disables the Display.
* @param None
* @retval None
*/
void hx8347g_DisplayOff(void)
{
/* Display Off */
hx8347g_WriteReg(LCD_REG_40, 0x38);
LCD_Delay(60);
hx8347g_WriteReg(LCD_REG_40, 0x04);
/* Power Off sequence ---------------------------------------------------------*/
hx8347g_WriteReg(LCD_REG_23, 0x0000); /* default 0x06 262k color, 0x05 65k color */
hx8347g_WriteReg(LCD_REG_24, 0x0000); /* Display frame rate = 70Hz RADJ = '0110' */
hx8347g_WriteReg(LCD_REG_25, 0x0000); /* OSC_EN = 1 */
hx8347g_WriteReg(LCD_REG_28, 0x0000); /* AP[2:0] = 111 */
hx8347g_WriteReg(LCD_REG_31, 0x0000); /* GAS=1, VOMG=00, PON=1, DK=0, XDK=0, DVDH_TRI=0, STB=0*/
hx8347g_WriteReg(LCD_REG_54, 0x0000); /* SS_PANEL = 1, GS_PANEL = 0,REV_PANEL = 0, BGR_PANEL = 1 */
}
/**
* @brief Get the LCD pixel Width.
* @param None
* @retval The Lcd Pixel Width
*/
uint16_t hx8347g_GetLcdPixelWidth(void)
{
return (uint16_t)HX8347G_LCD_PIXEL_WIDTH;
}
/**
* @brief Get the LCD pixel Height.
* @param None
* @retval The Lcd Pixel Height
*/
uint16_t hx8347g_GetLcdPixelHeight(void)
{
return (uint16_t)HX8347G_LCD_PIXEL_HEIGHT;
}
/**
* @brief Get the HX8347G ID.
* @param None
* @retval The HX8347G ID
*/
uint16_t hx8347g_ReadID(void)
{
if(Is_hx8347g_Initialized == 0)
{
LCD_IO_Init();
/* HX8347G requests 120ms (worst case) after reset */
LCD_Delay(120);
}
return (hx8347g_ReadReg(0x00));
}
/**
* @brief Set Cursor position.
* @param Xpos: specifies the X position.
* @param Ypos: specifies the Y position.
* @retval None
*/
void hx8347g_SetCursor(uint16_t Xpos, uint16_t Ypos)
{
hx8347g_WriteReg(LCD_REG_6, 0x00);
hx8347g_WriteReg(LCD_REG_7, Xpos);
hx8347g_WriteReg(LCD_REG_2, Ypos >> 8);
hx8347g_WriteReg(LCD_REG_3, Ypos & 0xFF);
}
/**
* @brief Write pixel.
* @param Xpos: specifies the X position.
* @param Ypos: specifies the Y position.
* @param RGBCode: the RGB pixel color
* @retval None
*/
void hx8347g_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode)
{
/* Set Cursor */
hx8347g_SetCursor(Xpos, Ypos);
/* Prepare to write GRAM */
LCD_IO_WriteReg(LCD_REG_34);
/* Write 16-bit GRAM Reg */
LCD_IO_WriteMultipleData((uint8_t*)&RGBCode, 2);
}
/**
* @brief Read pixel.
* @param None
* @retval the RGB pixel color
*/
uint16_t hx8347g_ReadPixel(uint16_t Xpos, uint16_t Ypos)
{
/* Set Cursor */
hx8347g_SetCursor(Xpos, Ypos);
/* Dummy read */
LCD_IO_ReadData(LCD_REG_34);
/* Read 16-bit Reg */
return (LCD_IO_ReadData(LCD_REG_34));
}
/**
* @brief Writes to the selected LCD register.
* @param LCDReg: address of the selected register.
* @param LCDRegValue: value to write to the selected register.
* @retval None
*/
void hx8347g_WriteReg(uint8_t LCDReg, uint16_t LCDRegValue)
{
LCD_IO_WriteReg(LCDReg);
/* Write 16-bit GRAM Reg */
LCD_IO_WriteMultipleData((uint8_t*)&LCDRegValue, 2);
}
/**
* @brief Reads the selected LCD Register.
* @param LCDReg: address of the selected register.
* @retval LCD Register Value.
*/
uint16_t hx8347g_ReadReg(uint8_t LCDReg)
{
/* Write 16-bit Index (then Read Reg) */
LCD_IO_WriteReg(LCDReg);
/* Read 16-bit Reg */
return (LCD_IO_ReadData(LCDReg));
}
/**
* @brief Sets a display window
* @param Xpos: specifies the X bottom left position.
* @param Ypos: specifies the Y bottom left position.
* @param Height: display window height.
* @param Width: display window width.
* @retval None
*/
void hx8347g_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
{
/* Horizontal GRAM Start Address */
hx8347g_WriteReg(LCD_REG_6, (Xpos) >> 8); /* SP */
hx8347g_WriteReg(LCD_REG_7, (Xpos) & 0xFF); /* SP */
/* Horizontal GRAM End Address */
hx8347g_WriteReg(LCD_REG_8, (Xpos + Height - 1) >> 8); /* EP */
hx8347g_WriteReg(LCD_REG_9, (Xpos + Height - 1) & 0xFF); /* EP */
/* Vertical GRAM Start Address */
hx8347g_WriteReg(LCD_REG_2, (Ypos) >> 8); /* SC */
hx8347g_WriteReg(LCD_REG_3, (Ypos) & 0xFF); /* SC */
/* Vertical GRAM End Address */
hx8347g_WriteReg(LCD_REG_4, (Ypos + Width - 1) >> 8); /* EC */
hx8347g_WriteReg(LCD_REG_5, (Ypos + Width - 1) & 0xFF); /* EC */
}
/**
* @brief Draw vertical line.
* @param RGBCode: Specifies the RGB color
* @param Xpos: specifies the X position.
* @param Ypos: specifies the Y position.
* @param Length: specifies the Line length.
* @retval None
*/
void hx8347g_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
{
uint32_t i = 0;
/* Set Cursor */
hx8347g_SetCursor(Xpos, Ypos);
/* Prepare to write GRAM */
LCD_IO_WriteReg(LCD_REG_34);
/* Sent a complete line */
for(i = 0; i < Length; i++)
{
ArrayRGB[i] = RGBCode;
}
LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
}
/**
* @brief Draw vertical line.
* @param RGBCode: Specifies the RGB color
* @param Xpos: specifies the X position.
* @param Ypos: specifies the Y position.
* @param Length: specifies the Line length.
* @retval None
*/
void hx8347g_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
{
uint16_t counter = 0;
/* Set Cursor */
hx8347g_SetCursor(Xpos, Ypos);
/* Prepare to write GRAM */
LCD_IO_WriteReg(LCD_REG_34);
/* Fill a complete vertical line */
for(counter = 0; counter < Length; counter++)
{
ArrayRGB[counter] = RGBCode;
}
/* Write 16-bit GRAM Reg */
LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
}
/**
* @brief Displays a bitmap picture loaded in the internal Flash.
* @param BmpAddress: Bmp picture address in the internal Flash.
* @retval None
*/
void hx8347g_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
{
uint32_t index = 0, size = 0;
/* Read bitmap size */
size = *(volatile uint16_t *) (pbmp + 2);
size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
/* Get bitmap data address offset */
index = *(volatile uint16_t *) (pbmp + 10);
index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
size = (size - index)/2;
pbmp += index;
/* Set GRAM write direction and BGR = 0 */
/* Memory access control: MY = 1, MX = 0, MV = 1, ML = 0 */
hx8347g_WriteReg(LCD_REG_22, 0xE0);
/* Set Cursor */
hx8347g_SetCursor(Xpos, Ypos);
/* Prepare to write GRAM */
LCD_IO_WriteReg(LCD_REG_34);
LCD_IO_WriteMultipleData((uint8_t*)pbmp, size*2);
/* Set GRAM write direction and BGR = 0 */
/* Memory access control: MY = 1, MX = 1, MV = 1, ML = 0 */
hx8347g_WriteReg(LCD_REG_22, 0xA0);
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/