1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
29
30
35
36
39
44
45
48
49
52
53
56
57
60
61
64
65
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
93
94
97
98
99
100
103
104
107
108
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
248
249
250
251
252
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
282
283
284
285
290
291
292
293
294
295
296
297
298
299
304
305
306
307
308
309
310
311
312
313
314
315
316
321
325
326
331
335
336
341
347
348
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
378
379
380
381
382
383
384
385
386
387
388
389
390
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
425
426
427
428
429
430
431
432
433
437
438
439
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
470
474
475
479
483
484
488
492
493
497
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
551
552
553
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
617
618
624
625
626
627
628
629
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
688
689
690
693
694
697
698
701
702
/* ... */
#include "st7789h2.h"
/* ... */
/* ... */
/* ... */
/* ... */
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
...} ST7789H2_Rgb888;
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
LCD_DrvTypeDef ST7789H2_drv =
{
ST7789H2_Init,
ST7789H2_ReadID,
ST7789H2_DisplayOn,
ST7789H2_DisplayOff,
ST7789H2_SetCursor,
ST7789H2_WritePixel,
ST7789H2_ReadPixel,
ST7789H2_SetDisplayWindow,
ST7789H2_DrawHLine,
ST7789H2_DrawVLine,
ST7789H2_GetLcdPixelWidth,
ST7789H2_GetLcdPixelHeight,
ST7789H2_DrawBitmap,
ST7789H2_DrawRGBImage,
...};
static uint16_t WindowsXstart = 0;
static uint16_t WindowsYstart = 0;
static uint16_t WindowsXend = ST7789H2_LCD_PIXEL_WIDTH-1;
static uint16_t WindowsYend = ST7789H2_LCD_PIXEL_HEIGHT-1;
/* ... */
/* ... */
static ST7789H2_Rgb888 ST7789H2_ReadPixel_rgb888(uint16_t Xpos, uint16_t Ypos);
static void ST7789H2_DrawRGBHLine(uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint8_t *pdata);
/* ... */
/* ... */
/* ... */
void ST7789H2_Init(void)
{
uint8_t parameter[14];
LCD_IO_Init();
ST7789H2_WriteReg(ST7789H2_SLEEP_IN, (uint8_t*)NULL, 0);
LCD_IO_Delay(10);
ST7789H2_WriteReg(0x01, (uint8_t*)NULL, 0);
LCD_IO_Delay(200);
ST7789H2_WriteReg(ST7789H2_SLEEP_OUT, (uint8_t*)NULL, 0);
LCD_IO_Delay(120);
parameter[0] = 0x00;
ST7789H2_WriteReg(ST7789H2_NORMAL_DISPLAY, parameter, 1);
parameter[0] = 0x05;
ST7789H2_WriteReg(ST7789H2_COLOR_MODE, parameter, 1);
ST7789H2_WriteReg(ST7789H2_DISPLAY_INVERSION, (uint8_t*)NULL, 0);
parameter[0] = 0x00;
parameter[1] = 0x00;
parameter[2] = 0x00;
parameter[3] = 0xEF;
ST7789H2_WriteReg(ST7789H2_CASET, parameter, 4);
parameter[0] = 0x00;
parameter[1] = 0x00;
parameter[2] = 0x00;
parameter[3] = 0xEF;
ST7789H2_WriteReg(ST7789H2_RASET, parameter, 4);
Initialize st7789h2 low level bus layer
parameter[0] = 0x0C;
parameter[1] = 0x0C;
parameter[2] = 0x00;
parameter[3] = 0x33;
parameter[4] = 0x33;
ST7789H2_WriteReg(ST7789H2_PORCH_CTRL, parameter, 5);
parameter[0] = 0x35;
ST7789H2_WriteReg(ST7789H2_GATE_CTRL, parameter, 1);
--------------- ST7789H2 Frame rate setting
parameter[0] = 0x1F;
ST7789H2_WriteReg(ST7789H2_VCOM_SET, parameter, 1);
parameter[0] = 0x2C;
ST7789H2_WriteReg(ST7789H2_LCM_CTRL, parameter, 1);
parameter[0] = 0x01;
parameter[1] = 0xC3;
ST7789H2_WriteReg(ST7789H2_VDV_VRH_EN, parameter, 2);
parameter[0] = 0x20;
ST7789H2_WriteReg(ST7789H2_VDV_SET, parameter, 1);
parameter[0] = 0x0F;
ST7789H2_WriteReg(ST7789H2_FR_CTRL, parameter, 1);
parameter[0] = 0xA4;
parameter[1] = 0xA1;
ST7789H2_WriteReg(ST7789H2_POWER_CTRL, parameter, 2);
--------------- ST7789H2 Power setting
parameter[0] = 0xD0;
parameter[1] = 0x08;
parameter[2] = 0x11;
parameter[3] = 0x08;
parameter[4] = 0x0C;
parameter[5] = 0x15;
parameter[6] = 0x39;
parameter[7] = 0x33;
parameter[8] = 0x50;
parameter[9] = 0x36;
parameter[10] = 0x13;
parameter[11] = 0x14;
parameter[12] = 0x29;
parameter[13] = 0x2D;
ST7789H2_WriteReg(ST7789H2_PV_GAMMA_CTRL, parameter, 14);
parameter[0] = 0xD0;
parameter[1] = 0x08;
parameter[2] = 0x10;
parameter[3] = 0x08;
parameter[4] = 0x06;
parameter[5] = 0x06;
parameter[6] = 0x39;
parameter[7] = 0x44;
parameter[8] = 0x51;
parameter[9] = 0x0B;
parameter[10] = 0x16;
parameter[11] = 0x14;
parameter[12] = 0x2F;
parameter[13] = 0x31;
ST7789H2_WriteReg(ST7789H2_NV_GAMMA_CTRL, parameter, 14);
ST7789H2_DisplayOn();
parameter[0] = 0x00;
ST7789H2_WriteReg(ST7789H2_TEARING_EFFECT, parameter, 1);
}{ ... }
/* ... */
void ST7789H2_SetOrientation(uint32_t orientation)
{
uint8_t parameter[6];
if(orientation == ST7789H2_ORIENTATION_LANDSCAPE)
{
parameter[0] = 0x00;
}if (orientation == ST7789H2_ORIENTATION_LANDSCAPE) { ... }
else if(orientation == ST7789H2_ORIENTATION_LANDSCAPE_ROT180)
{
parameter[0] = 0x00;
parameter[1] = 0x00;
parameter[2] = 0x01;
parameter[3] = 0xF0;
parameter[4] = 0x00;
parameter[5] = 0x00;
ST7789H2_WriteReg(ST7789H2_VSCRDEF, parameter, 6);
parameter[0] = 0x00;
parameter[1] = 0x50;
ST7789H2_WriteReg(ST7789H2_VSCSAD, parameter, 2);
parameter[0] = 0xC0;
}else if (orientation == ST7789H2_ORIENTATION_LANDSCAPE_ROT180) { ... }
else
{
parameter[0] = 0x60;
}else { ... }
ST7789H2_WriteReg(ST7789H2_NORMAL_DISPLAY, parameter, 1);
}{ ... }
/* ... */
void ST7789H2_DisplayOn(void)
{
ST7789H2_WriteReg(ST7789H2_DISPLAY_ON, (uint8_t*)NULL, 0);
ST7789H2_WriteReg(ST7789H2_SLEEP_OUT, (uint8_t*)NULL, 0);
}{ ... }
/* ... */
void ST7789H2_DisplayOff(void)
{
uint8_t parameter[1];
parameter[0] = 0xFE;
ST7789H2_WriteReg(ST7789H2_DISPLAY_OFF, parameter, 1);
ST7789H2_WriteReg(ST7789H2_SLEEP_IN, (uint8_t*)NULL, 0);
LCD_IO_Delay(10);
}{ ... }
/* ... */
uint16_t ST7789H2_GetLcdPixelWidth(void)
{
return (uint16_t)ST7789H2_LCD_PIXEL_WIDTH;
}{ ... }
/* ... */
uint16_t ST7789H2_GetLcdPixelHeight(void)
{
return (uint16_t)ST7789H2_LCD_PIXEL_HEIGHT;
}{ ... }
/* ... */
uint16_t ST7789H2_ReadID(void)
{
LCD_IO_Init();
return ST7789H2_ReadReg(ST7789H2_LCD_ID);
}{ ... }
/* ... */
void ST7789H2_SetCursor(uint16_t Xpos, uint16_t Ypos)
{
uint8_t parameter[4];
parameter[0] = 0x00;
parameter[1] = 0x00 + Xpos;
parameter[2] = 0x00;
parameter[3] = 0xEF + Xpos;
ST7789H2_WriteReg(ST7789H2_CASET, parameter, 4);
parameter[0] = 0x00;
parameter[1] = 0x00 + Ypos;
parameter[2] = 0x00;
parameter[3] = 0xEF + Ypos;
ST7789H2_WriteReg(ST7789H2_RASET, parameter, 4);
}{ ... }
/* ... */
void ST7789H2_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode)
{
ST7789H2_SetCursor(Xpos, Ypos);
ST7789H2_WriteReg(ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0);
LCD_IO_WriteData(RGBCode);
}{ ... }
/* ... */
uint16_t ST7789H2_ReadPixel(uint16_t Xpos, uint16_t Ypos)
{
ST7789H2_Rgb888 rgb888;
uint8_t r, g, b;
uint16_t rgb565;
ST7789H2_SetCursor(Xpos, Ypos);
rgb888 = ST7789H2_ReadPixel_rgb888(Xpos, Ypos);
r = ((rgb888.red & 0xF8) >> 3);
g = ((rgb888.green & 0xFC) >> 2);
b = ((rgb888.blue & 0xF8) >> 3);
rgb565 = ((uint16_t)(r) << 11) + ((uint16_t)(g) << 5) + ((uint16_t)(b) << 0);
return (rgb565);
}{ ... }
/* ... */
void ST7789H2_WriteReg(uint8_t Command, uint8_t *Parameters, uint8_t NbParameters)
{
uint8_t i;
LCD_IO_WriteReg(Command);
for (i=0; i<NbParameters; i++)
{
LCD_IO_WriteData(Parameters[i]);
}for (i=0; i
}{ ... }
/* ... */
uint8_t ST7789H2_ReadReg(uint8_t Command)
{
LCD_IO_WriteReg(Command);
LCD_IO_ReadData();
return (LCD_IO_ReadData());
}{ ... }
/* ... */
void ST7789H2_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
{
if (Xpos < ST7789H2_LCD_PIXEL_WIDTH)
{
WindowsXstart = Xpos;
}if (Xpos < ST7789H2_LCD_PIXEL_WIDTH) { ... }
else
{
WindowsXstart = 0;
}else { ... }
if (Ypos < ST7789H2_LCD_PIXEL_HEIGHT)
{
WindowsYstart = Ypos;
}if (Ypos < ST7789H2_LCD_PIXEL_HEIGHT) { ... }
else
{
WindowsYstart = 0;
}else { ... }
if (Width + Xpos <= ST7789H2_LCD_PIXEL_WIDTH)
{
WindowsXend = Width + Xpos - 1;
}if (Width + Xpos <= ST7789H2_LCD_PIXEL_WIDTH) { ... }
else
{
WindowsXend = ST7789H2_LCD_PIXEL_WIDTH - 1;
}else { ... }
if (Height + Ypos <= ST7789H2_LCD_PIXEL_HEIGHT)
{
WindowsYend = Height + Ypos - 1;
}if (Height + Ypos <= ST7789H2_LCD_PIXEL_HEIGHT) { ... }
else
{
WindowsYend = ST7789H2_LCD_PIXEL_HEIGHT-1;
}else { ... }
}{ ... }
/* ... */
void ST7789H2_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
{
uint16_t counter;
ST7789H2_SetCursor(Xpos, Ypos);
ST7789H2_WriteReg(ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0);
for(counter = 0; counter < Length; counter++)
{
LCD_IO_WriteData(RGBCode);
}for (counter = 0; counter < Length; counter++) { ... }
}{ ... }
/* ... */
void ST7789H2_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
{
uint16_t counter;
ST7789H2_SetCursor(Xpos, Ypos);
ST7789H2_WriteReg(ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0);
for(counter = 0; counter < Length; counter++)
{
ST7789H2_WritePixel(Xpos, Ypos + counter, RGBCode);
}for (counter = 0; counter < Length; counter++) { ... }
}{ ... }
/* ... */
void ST7789H2_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
{
uint32_t index = 0, size = 0;
uint32_t posY;
uint32_t nb_line = 0;
uint16_t Xsize = WindowsXend - WindowsXstart + 1;
uint16_t Ysize = WindowsYend - WindowsYstart + 1;
size = *(volatile uint16_t *) (pbmp + 2);
size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
index = *(volatile uint16_t *) (pbmp + 10);
index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
size = (size - index)/2;
pbmp += index;
for (posY = (Ypos + Ysize); posY > Ypos; posY--)
{
ST7789H2_SetCursor(Xpos, posY - 1);
ST7789H2_DrawRGBHLine(Xpos, posY - 1, Xsize, (pbmp + (nb_line * Xsize * 2)));
nb_line++;
...}
}{ ... }
/* ... */
void ST7789H2_DrawRGBImage(uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint16_t Ysize, uint8_t *pdata)
{
uint32_t posY;
uint32_t nb_line = 0;
for (posY = Ypos; posY < (Ypos + Ysize); posY ++)
{
ST7789H2_SetCursor(Xpos, posY);
ST7789H2_DrawRGBHLine(Xpos, posY, Xsize, (pdata + (nb_line * Xsize * 2)));
nb_line++;
}for (posY = Ypos; posY < (Ypos + Ysize); posY ++) { ... }
}{ ... }
/* ... */
/* ... */
static ST7789H2_Rgb888 ST7789H2_ReadPixel_rgb888(uint16_t Xpos, uint16_t Ypos)
{
ST7789H2_Rgb888 rgb888;
uint16_t rgb888_part1, rgb888_part2;
/* ... */
ST7789H2_SetCursor(Xpos, Ypos);
ST7789H2_WriteReg(ST7789H2_READ_RAM, (uint8_t*)NULL, 0);
LCD_IO_ReadData();
rgb888_part1 = LCD_IO_ReadData();
rgb888_part2 = LCD_IO_ReadData();
rgb888.red = (rgb888_part1 & 0xFC00) >> 8;
rgb888.green = (rgb888_part1 & 0x00FC) >> 0;
rgb888.blue = (rgb888_part2 & 0xFC00) >> 8;
return rgb888;
}{ ... }
/* ... */
static void ST7789H2_DrawRGBHLine(uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint8_t *pdata)
{
uint32_t i = 0;
uint32_t posX;
uint16_t *rgb565 = (uint16_t*)pdata;
ST7789H2_WriteReg(ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0);
for (posX = Xpos; posX < (Xsize + Xpos); posX++)
{
if ((posX >= WindowsXstart) && (Ypos >= WindowsYstart) &&
(posX <= WindowsXend) && (Ypos <= WindowsYend))
{
if (posX != (Xsize + Xpos))
{
LCD_IO_WriteData(rgb565[i]);
...}
i++;
}if ((posX >= WindowsXstart) && (Ypos >= WindowsYstart) && /* Check we are in the defined window */ (posX <= WindowsXend) && (Ypos <= WindowsYend)) { ... }
}for (posX = Xpos; posX < (Xsize + Xpos); posX++) { ... }
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */