LibDriver ST7789
Loading...
Searching...
No Matches
driver_st7789.c
Go to the documentation of this file.
1
36
37#include "driver_st7789.h"
38#include "driver_st7789_font.h"
39
43#define CHIP_NAME "Sitronix ST7789"
44#define MANUFACTURER_NAME "Sitronix"
45#define SUPPLY_VOLTAGE_MIN 2.4f
46#define SUPPLY_VOLTAGE_MAX 3.3f
47#define MAX_CURRENT 7.5f
48#define TEMPERATURE_MIN -30.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 1000
51
55#define ST7789_CMD 0
56#define ST7789_DATA 1
57
61#define ST7789_CMD_NOP 0x00
62#define ST7789_CMD_SWRESET 0x01
63#define ST7789_CMD_SLPIN 0x10
64#define ST7789_CMD_SLPOUT 0x11
65#define ST7789_CMD_PTLON 0x12
66#define ST7789_CMD_NORON 0x13
67#define ST7789_CMD_INVOFF 0x20
68#define ST7789_CMD_INVON 0x21
69#define ST7789_CMD_GAMSET 0x26
70#define ST7789_CMD_DISPOFF 0x28
71#define ST7789_CMD_DISPON 0x29
72#define ST7789_CMD_CASET 0x2A
73#define ST7789_CMD_RASET 0x2B
74#define ST7789_CMD_RAMWR 0x2C
75#define ST7789_CMD_PTLAR 0x30
76#define ST7789_CMD_VSCRDEF 0x33
77#define ST7789_CMD_TEOFF 0x34
78#define ST7789_CMD_TEON 0x35
79#define ST7789_CMD_MADCTL 0x36
80#define ST7789_CMD_VSCRSADD 0x37
81#define ST7789_CMD_IDMOFF 0x38
82#define ST7789_CMD_IDMON 0x39
83#define ST7789_CMD_COLMOD 0x3A
84#define ST7789_CMD_RAMWRC 0x3C
85#define ST7789_CMD_TESCAN 0x44
86#define ST7789_CMD_WRDISBV 0x51
87#define ST7789_CMD_WRCTRLD 0x53
88#define ST7789_CMD_WRCACE 0x55
89#define ST7789_CMD_WRCABCMB 0x5E
90#define ST7789_CMD_RAMCTRL 0xB0
91#define ST7789_CMD_RGBCTRL 0xB1
92#define ST7789_CMD_PORCTRL 0xB2
93#define ST7789_CMD_FRCTRL1 0xB3
94#define ST7789_CMD_PARCTRL 0xB5
95#define ST7789_CMD_GCTRL 0xB7
96#define ST7789_CMD_GTADJ 0xB8
97#define ST7789_CMD_DGMEN 0xBA
98#define ST7789_CMD_VCOMS 0xBB
99#define ST7789_CMD_LCMCTRL 0xC0
100#define ST7789_CMD_IDSET 0xC1
101#define ST7789_CMD_VDVVRHEN 0xC2
102#define ST7789_CMD_VRHS 0xC3
103#define ST7789_CMD_VDVSET 0xC4
104#define ST7789_CMD_VCMOFSET 0xC5
105#define ST7789_CMD_FRCTR2 0xC6
106#define ST7789_CMD_CABCCTRL 0xC7
107#define ST7789_CMD_REGSEL1 0xC8
108#define ST7789_CMD_REGSEL2 0xCA
109#define ST7789_CMD_PWMFRSEL 0xCC
110#define ST7789_CMD_PWCTRL1 0xD0
111#define ST7789_CMD_VAPVANEN 0xD2
112#define ST7789_CMD_CMD2EN 0xDF
113#define ST7789_CMD_PVGAMCTRL 0xE0
114#define ST7789_CMD_NVGAMCTRL 0xE1
115#define ST7789_CMD_DGMLUTR 0xE2
116#define ST7789_CMD_DGMLUTB 0xE3
117#define ST7789_CMD_GATECTRL 0xE4
118#define ST7789_CMD_SPI2EN 0xE7
119#define ST7789_CMD_PWCTRL2 0xE8
120#define ST7789_CMD_EQCTRL 0xE9
121#define ST7789_CMD_PROMCTRL 0xEC
122#define ST7789_CMD_PROMEN 0xFA
123#define ST7789_CMD_NVMSET 0xFC
124#define ST7789_CMD_PROMACT 0xFE
125
136static uint8_t a_st7789_write_byte(st7789_handle_t *handle, uint8_t data, uint8_t cmd)
137{
138 uint8_t res;
139
140 res = handle->cmd_data_gpio_write(cmd); /* write gpio */
141 if (res != 0) /* check result */
142 {
143 return 1; /* return error */
144 }
145 res = handle->spi_write_cmd(&data, 1); /* write data command */
146 if (res != 0) /* check result */
147 {
148 return 1; /* return error */
149 }
150
151 return 0; /* success return 0 */
152}
153
165static uint8_t a_st7789_write_bytes(st7789_handle_t *handle, uint8_t *data, uint16_t len, uint8_t cmd)
166{
167 uint8_t res;
168
169 res = handle->cmd_data_gpio_write(cmd); /* write gpio */
170 if (res != 0) /* check result */
171 {
172 return 1; /* return error */
173 }
174 res = handle->spi_write_cmd(data, len); /* write data command */
175 if (res != 0) /* check result */
176 {
177 return 1; /* return error */
178 }
179
180 return 0; /* success return 0 */
181}
182
194{
195 if (handle == NULL) /* check handle */
196 {
197 return 2; /* return error */
198 }
199 if (handle->inited != 1) /* check handle initialization */
200 {
201 return 3; /* return error */
202 }
203
204 if (a_st7789_write_byte(handle, ST7789_CMD_NOP, ST7789_CMD) != 0) /* write nop command */
205 {
206 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
207
208 return 1; /* return error */
209 }
210
211 return 0; /* success return 0 */
212}
213
225{
226 if (handle == NULL) /* check handle */
227 {
228 return 2; /* return error */
229 }
230 if (handle->inited != 1) /* check handle initialization */
231 {
232 return 3; /* return error */
233 }
234
235 if (a_st7789_write_byte(handle, ST7789_CMD_SWRESET, ST7789_CMD) != 0) /* write software reset command */
236 {
237 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
238
239 return 1; /* return error */
240 }
241 handle->delay_ms(200); /* delay 200ms */
242
243 return 0; /* success return 0 */
244}
245
257{
258 if (handle == NULL) /* check handle */
259 {
260 return 2; /* return error */
261 }
262 if (handle->inited != 1) /* check handle initialization */
263 {
264 return 3; /* return error */
265 }
266
267 if (a_st7789_write_byte(handle, ST7789_CMD_SLPIN, ST7789_CMD) != 0) /* write sleep in command */
268 {
269 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
270
271 return 1; /* return error */
272 }
273
274 return 0; /* success return 0 */
275}
276
288{
289 if (handle == NULL) /* check handle */
290 {
291 return 2; /* return error */
292 }
293 if (handle->inited != 1) /* check handle initialization */
294 {
295 return 3; /* return error */
296 }
297
298 if (a_st7789_write_byte(handle, ST7789_CMD_SLPOUT, ST7789_CMD) != 0) /* write sleep out command */
299 {
300 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
301
302 return 1; /* return error */
303 }
304 handle->delay_ms(200); /* delay 200ms */
305
306 return 0; /* success return 0 */
307}
308
320{
321 if (handle == NULL) /* check handle */
322 {
323 return 2; /* return error */
324 }
325 if (handle->inited != 1) /* check handle initialization */
326 {
327 return 3; /* return error */
328 }
329
330 if (a_st7789_write_byte(handle, ST7789_CMD_PTLON, ST7789_CMD) != 0) /* write partial display mode on command */
331 {
332 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
333
334 return 1; /* return error */
335 }
336
337 return 0; /* success return 0 */
338}
339
351{
352 if (handle == NULL) /* check handle */
353 {
354 return 2; /* return error */
355 }
356 if (handle->inited != 1) /* check handle initialization */
357 {
358 return 3; /* return error */
359 }
360
361 if (a_st7789_write_byte(handle, ST7789_CMD_NORON, ST7789_CMD) != 0) /* write normal display mode on command */
362 {
363 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
364
365 return 1; /* return error */
366 }
367
368 return 0; /* success return 0 */
369}
370
382{
383 if (handle == NULL) /* check handle */
384 {
385 return 2; /* return error */
386 }
387 if (handle->inited != 1) /* check handle initialization */
388 {
389 return 3; /* return error */
390 }
391
392 if (a_st7789_write_byte(handle, ST7789_CMD_INVOFF, ST7789_CMD) != 0) /* write display inversion off command */
393 {
394 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
395
396 return 1; /* return error */
397 }
398
399 return 0; /* success return 0 */
400}
401
413{
414 if (handle == NULL) /* check handle */
415 {
416 return 2; /* return error */
417 }
418 if (handle->inited != 1) /* check handle initialization */
419 {
420 return 3; /* return error */
421 }
422
423 if (a_st7789_write_byte(handle, ST7789_CMD_INVON, ST7789_CMD) != 0) /* write display inversion on command */
424 {
425 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
426
427 return 1; /* return error */
428 }
429
430 return 0; /* success return 0 */
431}
432
444uint8_t st7789_set_gamma(st7789_handle_t *handle, uint8_t gamma)
445{
446 if (handle == NULL) /* check handle */
447 {
448 return 2; /* return error */
449 }
450 if (handle->inited != 1) /* check handle initialization */
451 {
452 return 3; /* return error */
453 }
454
455 if (a_st7789_write_byte(handle, ST7789_CMD_GAMSET, ST7789_CMD) != 0) /* write set gamma command */
456 {
457 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
458
459 return 1; /* return error */
460 }
461 if (a_st7789_write_byte(handle, gamma & 0x0F, ST7789_DATA) != 0) /* write gamma data */
462 {
463 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
464
465 return 1; /* return error */
466 }
467
468 return 0; /* success return 0 */
469}
470
482{
483 if (handle == NULL) /* check handle */
484 {
485 return 2; /* return error */
486 }
487 if (handle->inited != 1) /* check handle initialization */
488 {
489 return 3; /* return error */
490 }
491
492 if (a_st7789_write_byte(handle, ST7789_CMD_DISPOFF, ST7789_CMD) != 0) /* write display off command */
493 {
494 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
495
496 return 1; /* return error */
497 }
498
499 return 0; /* success return 0 */
500}
501
513{
514 if (handle == NULL) /* check handle */
515 {
516 return 2; /* return error */
517 }
518 if (handle->inited != 1) /* check handle initialization */
519 {
520 return 3; /* return error */
521 }
522
523 if (a_st7789_write_byte(handle, ST7789_CMD_DISPON, ST7789_CMD) != 0) /* write display on command */
524 {
525 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
526
527 return 1; /* return error */
528 }
529
530 return 0; /* success return 0 */
531}
532
547uint8_t st7789_set_column_address(st7789_handle_t *handle, uint16_t start_address, uint16_t end_address)
548{
549 uint8_t buf[4];
550
551 if (handle == NULL) /* check handle */
552 {
553 return 2; /* return error */
554 }
555 if (handle->inited != 1) /* check handle initialization */
556 {
557 return 3; /* return error */
558 }
559 if ((start_address > 319) || (end_address > 319)) /* check range */
560 {
561 handle->debug_print("st7789: address is invalid.\n"); /* address is invalid */
562
563 return 4; /* return error */
564 }
565 if (start_address >= end_address) /* check range */
566 {
567 handle->debug_print("st7789: start_address >= end_address.\n"); /* start_address >= end_address */
568
569 return 5; /* return error */
570 }
571
572 if (a_st7789_write_byte(handle, ST7789_CMD_CASET, ST7789_CMD) != 0) /* write set column address command */
573 {
574 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
575
576 return 1; /* return error */
577 }
578 buf[0] = (start_address >> 8) & 0xFF; /* start address msb */
579 buf[1] = (start_address >> 0) & 0xFF; /* start address lsb */
580 buf[2] = (end_address >> 8) & 0xFF; /* end address msb */
581 buf[3] = (end_address >> 0) & 0xFF; /* end address lsb */
582 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
583 {
584 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
585
586 return 1; /* return error */
587 }
588
589 return 0; /* success return 0 */
590}
591
606uint8_t st7789_set_row_address(st7789_handle_t *handle, uint16_t start_address, uint16_t end_address)
607{
608 uint8_t buf[4];
609
610 if (handle == NULL) /* check handle */
611 {
612 return 2; /* return error */
613 }
614 if (handle->inited != 1) /* check handle initialization */
615 {
616 return 3; /* return error */
617 }
618 if ((start_address > 319) || (end_address > 319)) /* check range */
619 {
620 handle->debug_print("st7789: address is invalid.\n"); /* address is invalid */
621
622 return 4; /* return error */
623 }
624 if (start_address >= end_address) /* check range */
625 {
626 handle->debug_print("st7789: start_address >= end_address.\n"); /* start_address >= end_address */
627
628 return 5; /* return error */
629 }
630
631 if (a_st7789_write_byte(handle, ST7789_CMD_RASET, ST7789_CMD) != 0) /* write set row address command */
632 {
633 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
634
635 return 1; /* return error */
636 }
637 buf[0] = (start_address >> 8) & 0xFF; /* start address msb */
638 buf[1] = (start_address >> 0) & 0xFF; /* start address lsb */
639 buf[2] = (end_address >> 8) & 0xFF; /* end address msb */
640 buf[3] = (end_address >> 0) & 0xFF; /* end address lsb */
641 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
642 {
643 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
644
645 return 1; /* return error */
646 }
647
648 return 0; /* success return 0 */
649}
650
663uint8_t st7789_memory_write(st7789_handle_t *handle, uint8_t *data, uint16_t len)
664{
665 if (handle == NULL) /* check handle */
666 {
667 return 2; /* return error */
668 }
669 if (handle->inited != 1) /* check handle initialization */
670 {
671 return 3; /* return error */
672 }
673
674 if (a_st7789_write_byte(handle, ST7789_CMD_RAMWR, ST7789_CMD) != 0) /* write memory write command */
675 {
676 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
677
678 return 1; /* return error */
679 }
680 if (a_st7789_write_bytes(handle, data, len, ST7789_DATA) != 0) /* write data */
681 {
682 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
683
684 return 1; /* return error */
685 }
686
687 return 0; /* success return 0 */
688}
689
702uint8_t st7789_set_partial_areas(st7789_handle_t *handle, uint16_t start_row, uint16_t end_row)
703{
704 uint8_t buf[4];
705
706 if (handle == NULL) /* check handle */
707 {
708 return 2; /* return error */
709 }
710 if (handle->inited != 1) /* check handle initialization */
711 {
712 return 3; /* return error */
713 }
714
715 if (a_st7789_write_byte(handle, ST7789_CMD_PTLAR, ST7789_CMD) != 0) /* write set partial areas command */
716 {
717 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
718
719 return 1; /* return error */
720 }
721 buf[0] = (start_row >> 8) & 0xFF; /* start row msb */
722 buf[1] = (start_row >> 0) & 0xFF; /* start row lsb */
723 buf[2] = (end_row >> 8) & 0xFF; /* end row msb */
724 buf[3] = (end_row >> 0) & 0xFF; /* end row lsb */
725 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
726 {
727 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
728
729 return 1; /* return error */
730 }
731
732 return 0; /* success return 0 */
733}
734
748uint8_t st7789_set_vertical_scrolling(st7789_handle_t *handle, uint16_t top_fixed_area,
749 uint16_t scrolling_area, uint16_t bottom_fixed_area)
750{
751 uint8_t buf[6];
752
753 if (handle == NULL) /* check handle */
754 {
755 return 2; /* return error */
756 }
757 if (handle->inited != 1) /* check handle initialization */
758 {
759 return 3; /* return error */
760 }
761
762 if (a_st7789_write_byte(handle, ST7789_CMD_VSCRDEF, ST7789_CMD) != 0) /* write set vertical scrolling definition command */
763 {
764 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
765
766 return 1; /* return error */
767 }
768 buf[0] = (top_fixed_area >> 8) & 0xFF; /* top fixed area msb */
769 buf[1] = (top_fixed_area >> 0) & 0xFF; /* top fixed area lsb */
770 buf[2] = (scrolling_area >> 8) & 0xFF; /* scrolling area msb */
771 buf[3] = (scrolling_area >> 0) & 0xFF; /* scrolling area lsb */
772 buf[4] = (bottom_fixed_area >> 8) & 0xFF; /* bottom fixed area msb */
773 buf[5] = (bottom_fixed_area >> 0) & 0xFF; /* bottom fixed area lsb */
774 if (a_st7789_write_bytes(handle, buf, 6, ST7789_DATA) != 0) /* write data */
775 {
776 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
777
778 return 1; /* return error */
779 }
780
781 return 0; /* success return 0 */
782}
783
795{
796 if (handle == NULL) /* check handle */
797 {
798 return 2; /* return error */
799 }
800 if (handle->inited != 1) /* check handle initialization */
801 {
802 return 3; /* return error */
803 }
804
805 if (a_st7789_write_byte(handle, ST7789_CMD_TEOFF, ST7789_CMD) != 0) /* write tearing effect line off command */
806 {
807 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
808
809 return 1; /* return error */
810 }
811
812 return 0; /* success return 0 */
813}
814
827{
828 if (handle == NULL) /* check handle */
829 {
830 return 2; /* return error */
831 }
832 if (handle->inited != 1) /* check handle initialization */
833 {
834 return 3; /* return error */
835 }
836
837 if (a_st7789_write_byte(handle, ST7789_CMD_TEON, ST7789_CMD) != 0) /* write tearing effect line off command */
838 {
839 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
840
841 return 1; /* return error */
842 }
843 if (a_st7789_write_byte(handle, effect, ST7789_DATA) != 0) /* write data */
844 {
845 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
846
847 return 1; /* return error */
848 }
849
850 return 0; /* success return 0 */
851}
852
865{
866 if (handle == NULL) /* check handle */
867 {
868 return 2; /* return error */
869 }
870 if (handle->inited != 1) /* check handle initialization */
871 {
872 return 3; /* return error */
873 }
874
875 if (a_st7789_write_byte(handle, ST7789_CMD_MADCTL, ST7789_CMD) != 0) /* write set memory data access control command */
876 {
877 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
878
879 return 1; /* return error */
880 }
881 if (a_st7789_write_byte(handle, order, ST7789_DATA) != 0) /* write data */
882 {
883 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
884
885 return 1; /* return error */
886 }
887
888 return 0; /* success return 0 */
889}
890
903uint8_t st7789_set_vertical_scroll_start_address(st7789_handle_t *handle, uint16_t start_address)
904{
905 uint8_t buf[2];
906
907 if (handle == NULL) /* check handle */
908 {
909 return 2; /* return error */
910 }
911 if (handle->inited != 1) /* check handle initialization */
912 {
913 return 3; /* return error */
914 }
915 if (start_address > 319) /* check range */
916 {
917 handle->debug_print("st7789: address is invalid.\n"); /* address is invalid */
918
919 return 4; /* return error */
920 }
921
922 if (a_st7789_write_byte(handle, ST7789_CMD_VSCRSADD, ST7789_CMD) != 0) /* write vertical scrolling start address command */
923 {
924 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
925
926 return 1; /* return error */
927 }
928 buf[0] = (start_address >> 8) & 0xFF; /* start address msb */
929 buf[1] = (start_address >> 0) & 0xFF; /* start address lsb */
930 if (a_st7789_write_bytes(handle, buf, 2, ST7789_DATA) != 0) /* write data */
931 {
932 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
933
934 return 1; /* return error */
935 }
936
937 return 0; /* success return 0 */
938}
939
951{
952 if (handle == NULL) /* check handle */
953 {
954 return 2; /* return error */
955 }
956 if (handle->inited != 1) /* check handle initialization */
957 {
958 return 3; /* return error */
959 }
960
961 if (a_st7789_write_byte(handle, ST7789_CMD_IDMOFF, ST7789_CMD) != 0) /* write idle mode off command */
962 {
963 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
964
965 return 1; /* return error */
966 }
967
968 return 0; /* success return 0 */
969}
970
982{
983 if (handle == NULL) /* check handle */
984 {
985 return 2; /* return error */
986 }
987 if (handle->inited != 1) /* check handle initialization */
988 {
989 return 3; /* return error */
990 }
991
992 if (a_st7789_write_byte(handle, ST7789_CMD_IDMON, ST7789_CMD) != 0) /* write idle mode on command */
993 {
994 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
995
996 return 1; /* return error */
997 }
998
999 return 0; /* success return 0 */
1000}
1001
1016{
1017 uint8_t data;
1018
1019 if (handle == NULL) /* check handle */
1020 {
1021 return 2; /* return error */
1022 }
1023 if (handle->inited != 1) /* check handle initialization */
1024 {
1025 return 3; /* return error */
1026 }
1027
1028 if (a_st7789_write_byte(handle, ST7789_CMD_COLMOD, ST7789_CMD) != 0) /* write interface pixel format command */
1029 {
1030 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1031
1032 return 1; /* return error */
1033 }
1034
1035 data = (rgb << 4) | (control << 0); /* set pixel format */
1036 if (a_st7789_write_byte(handle, data, ST7789_DATA) != 0) /* write data */
1037 {
1038 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1039
1040 return 1; /* return error */
1041 }
1042 handle->format = data; /* set format */
1043
1044 return 0; /* success return 0 */
1045}
1046
1059uint8_t st7789_memory_continue_write(st7789_handle_t *handle, uint8_t *data, uint16_t len)
1060{
1061 if (handle == NULL) /* check handle */
1062 {
1063 return 2; /* return error */
1064 }
1065 if (handle->inited != 1) /* check handle initialization */
1066 {
1067 return 3; /* return error */
1068 }
1069
1070 if (a_st7789_write_byte(handle, ST7789_CMD_RAMWRC, ST7789_CMD) != 0) /* write memory write continue command */
1071 {
1072 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1073
1074 return 1; /* return error */
1075 }
1076 if (a_st7789_write_bytes(handle, data, len, ST7789_DATA) != 0) /* write data */
1077 {
1078 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1079
1080 return 1; /* return error */
1081 }
1082
1083 return 0; /* success return 0 */
1084}
1085
1097uint8_t st7789_set_tear_scanline(st7789_handle_t *handle, uint16_t l)
1098{
1099 uint8_t buf[2];
1100
1101 if (handle == NULL) /* check handle */
1102 {
1103 return 2; /* return error */
1104 }
1105 if (handle->inited != 1) /* check handle initialization */
1106 {
1107 return 3; /* return error */
1108 }
1109
1110 if (a_st7789_write_byte(handle, ST7789_CMD_TESCAN, ST7789_CMD) != 0) /* write set tear scanline command */
1111 {
1112 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1113
1114 return 1; /* return error */
1115 }
1116 buf[0] = (l >> 8) & 0xFF; /* start line msb */
1117 buf[1] = (l >> 0) & 0xFF; /* start line lsb */
1118 if (a_st7789_write_bytes(handle, buf, 2, ST7789_DATA) != 0) /* write data */
1119 {
1120 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1121
1122 return 1; /* return error */
1123 }
1124
1125 return 0; /* success return 0 */
1126}
1127
1139uint8_t st7789_set_display_brightness(st7789_handle_t *handle, uint8_t brightness)
1140{
1141 if (handle == NULL) /* check handle */
1142 {
1143 return 2; /* return error */
1144 }
1145 if (handle->inited != 1) /* check handle initialization */
1146 {
1147 return 3; /* return error */
1148 }
1149
1150 if (a_st7789_write_byte(handle, ST7789_CMD_WRDISBV, ST7789_CMD) != 0) /* write display brightness command */
1151 {
1152 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1153
1154 return 1; /* return error */
1155 }
1156
1157 if (a_st7789_write_byte(handle, brightness, ST7789_DATA) != 0) /* write brightness */
1158 {
1159 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1160
1161 return 1; /* return error */
1162 }
1163
1164 return 0; /* success return 0 */
1165}
1166
1180uint8_t st7789_set_display_control(st7789_handle_t *handle, st7789_bool_t brightness_control_block,
1181 st7789_bool_t display_dimming, st7789_bool_t backlight_control)
1182{
1183 uint8_t data;
1184
1185 if (handle == NULL) /* check handle */
1186 {
1187 return 2; /* return error */
1188 }
1189 if (handle->inited != 1) /* check handle initialization */
1190 {
1191 return 3; /* return error */
1192 }
1193
1194 if (a_st7789_write_byte(handle, ST7789_CMD_WRCTRLD, ST7789_CMD) != 0) /* write CTRL display command */
1195 {
1196 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1197
1198 return 1; /* return error */
1199 }
1200
1201 data = (brightness_control_block << 5) | (display_dimming << 3)
1202 | (backlight_control << 2); /* set control data */
1203 if (a_st7789_write_byte(handle, data, ST7789_DATA) != 0) /* write control */
1204 {
1205 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1206
1207 return 1; /* return error */
1208 }
1209
1210 return 0; /* success return 0 */
1211}
1212
1228{
1229 uint8_t data;
1230
1231 if (handle == NULL) /* check handle */
1232 {
1233 return 2; /* return error */
1234 }
1235 if (handle->inited != 1) /* check handle initialization */
1236 {
1237 return 3; /* return error */
1238 }
1239
1240 if (a_st7789_write_byte(handle, ST7789_CMD_WRCACE, ST7789_CMD) != 0) /* write content adaptive brightness control and color enhancement command */
1241 {
1242 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1243
1244 return 1; /* return error */
1245 }
1246
1247 data = (color_enhancement << 7) | (level << 4) | (mode << 0); /* set control data */
1248 if (a_st7789_write_byte(handle, data, ST7789_DATA) != 0) /* write control */
1249 {
1250 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1251
1252 return 1; /* return error */
1253 }
1254
1255 return 0; /* success return 0 */
1256}
1257
1269uint8_t st7789_set_cabc_minimum_brightness(st7789_handle_t *handle, uint8_t brightness)
1270{
1271 if (handle == NULL) /* check handle */
1272 {
1273 return 2; /* return error */
1274 }
1275 if (handle->inited != 1) /* check handle initialization */
1276 {
1277 return 3; /* return error */
1278 }
1279
1280 if (a_st7789_write_byte(handle, ST7789_CMD_WRCABCMB, ST7789_CMD) != 0) /* write CABC minimum brightness command */
1281 {
1282 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1283
1284 return 1; /* return error */
1285 }
1286
1287 if (a_st7789_write_byte(handle, brightness, ST7789_DATA) != 0) /* write brightness */
1288 {
1289 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1290
1291 return 1; /* return error */
1292 }
1293
1294 return 0; /* success return 0 */
1295}
1296
1314 st7789_ram_access_t ram_mode,
1315 st7789_display_mode_t display_mode,
1316 st7789_frame_type_t frame_type,
1317 st7789_data_mode_t data_mode,
1318 st7789_rgb_bus_width_t bus_width,
1319 st7789_pixel_type_t pixel_type)
1320{
1321 uint8_t buf[2];
1322
1323 if (handle == NULL) /* check handle */
1324 {
1325 return 2; /* return error */
1326 }
1327 if (handle->inited != 1) /* check handle initialization */
1328 {
1329 return 3; /* return error */
1330 }
1331
1332 if (a_st7789_write_byte(handle, ST7789_CMD_RAMCTRL, ST7789_CMD) != 0) /* write ram control command */
1333 {
1334 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1335
1336 return 1; /* return error */
1337 }
1338 buf[0] = (ram_mode << 4) | (display_mode << 0); /* set param1 */
1339 buf[1] = (frame_type << 4) | (data_mode << 3) |
1340 (bus_width << 2) | (pixel_type << 0); /* set param2 */
1341 if (a_st7789_write_bytes(handle, buf, 2, ST7789_DATA) != 0) /* write data */
1342 {
1343 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1344
1345 return 1; /* return error */
1346 }
1347
1348 return 0; /* success return 0 */
1349}
1350
1373 st7789_direct_rgb_mode_t rgb_mode,
1374 st7789_rgb_if_enable_mode_t rgb_if_mode,
1375 st7789_pin_level_t vspl,
1376 st7789_pin_level_t hspl,
1379 uint8_t vbp,
1380 uint8_t hbp)
1381{
1382 uint8_t buf[3];
1383
1384 if (handle == NULL) /* check handle */
1385 {
1386 return 2; /* return error */
1387 }
1388 if (handle->inited != 1) /* check handle initialization */
1389 {
1390 return 3; /* return error */
1391 }
1392 if (vbp > 0x7F) /* check vbp */
1393 {
1394 handle->debug_print("st7789: vbp > 0x7F.\n"); /* vbp > 0x7F */
1395
1396 return 4; /* return error */
1397 }
1398 if (hbp > 0x1F) /* check hbp */
1399 {
1400 handle->debug_print("st7789: hbp > 0x1F.\n"); /* hbp > 0x1F */
1401
1402 return 5; /* return error */
1403 }
1404
1405 if (a_st7789_write_byte(handle, ST7789_CMD_RGBCTRL, ST7789_CMD) != 0) /* write rgb control command */
1406 {
1407 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1408
1409 return 1; /* return error */
1410 }
1411 buf[0] = (rgb_mode << 7) | (rgb_if_mode << 5) |
1412 (vspl << 3) | (hspl << 2) | (dpl << 1) | (epl << 0); /* set param1 */
1413 buf[1] = vbp & 0x7F; /* set param2 */
1414 buf[2] = hbp & 0x1F; /* set param3 */
1415 if (a_st7789_write_bytes(handle, buf, 3, ST7789_DATA) != 0) /* write data */
1416 {
1417 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1418
1419 return 1; /* return error */
1420 }
1421
1422 return 0; /* success return 0 */
1423}
1424
1454 uint8_t back_porch_normal,
1455 uint8_t front_porch_normal,
1456 st7789_bool_t separate_porch_enable,
1457 uint8_t back_porch_idle,
1458 uint8_t front_porch_idle,
1459 uint8_t back_porch_partial,
1460 uint8_t front_porch_partial)
1461{
1462 uint8_t buf[5];
1463
1464 if (handle == NULL) /* check handle */
1465 {
1466 return 2; /* return error */
1467 }
1468 if (handle->inited != 1) /* check handle initialization */
1469 {
1470 return 3; /* return error */
1471 }
1472 if (back_porch_normal > 0x7F) /* check back_porch_normal */
1473 {
1474 handle->debug_print("st7789: back_porch_normal > 0x7F.\n"); /* back_porch_normal > 0x7F */
1475
1476 return 4; /* return error */
1477 }
1478 if (front_porch_normal > 0x7F) /* check front_porch_normal */
1479 {
1480 handle->debug_print("st7789: front_porch_normal > 0x7F.\n"); /* front_porch_normal > 0x7F */
1481
1482 return 5; /* return error */
1483 }
1484 if (back_porch_idle > 0xF) /* check back_porch_idle */
1485 {
1486 handle->debug_print("st7789: back_porch_idle > 0xF.\n"); /* back_porch_idle > 0xF */
1487
1488 return 6; /* return error */
1489 }
1490 if (front_porch_idle > 0xF) /* check front_porch_idle */
1491 {
1492 handle->debug_print("st7789: front_porch_idle > 0xF.\n"); /* front_porch_idle > 0xF */
1493
1494 return 7; /* return error */
1495 }
1496 if (back_porch_partial > 0xF) /* check back_porch_partial */
1497 {
1498 handle->debug_print("st7789: back_porch_partial > 0xF.\n"); /* back_porch_partial > 0xF */
1499
1500 return 8; /* return error */
1501 }
1502 if (front_porch_partial > 0xF) /* check front_porch_partial */
1503 {
1504 handle->debug_print("st7789: front_porch_partial > 0xF.\n"); /* front_porch_partial > 0xF */
1505
1506 return 9; /* return error */
1507 }
1508
1509 if (a_st7789_write_byte(handle, ST7789_CMD_PORCTRL, ST7789_CMD) != 0) /* write porch control command */
1510 {
1511 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1512
1513 return 1; /* return error */
1514 }
1515 buf[0] = back_porch_normal; /* set param1 */
1516 buf[1] = front_porch_normal; /* set param2 */
1517 buf[2] = separate_porch_enable; /* set param3 */
1518 buf[3] = (back_porch_idle & 0xF) << 4 | (front_porch_idle & 0xF); /* set param4 */
1519 buf[4] = (back_porch_partial & 0xF) << 4 | (front_porch_partial & 0xF); /* set param5 */
1520 if (a_st7789_write_bytes(handle, buf, 5, ST7789_DATA) != 0) /* write data */
1521 {
1522 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1523
1524 return 1; /* return error */
1525 }
1526
1527 return 0; /* success return 0 */
1528}
1529
1550 st7789_bool_t separate_fr_control,
1553 uint8_t idle_frame_rate,
1555 uint8_t partial_frame_rate)
1556{
1557 uint8_t buf[3];
1558
1559 if (handle == NULL) /* check handle */
1560 {
1561 return 2; /* return error */
1562 }
1563 if (handle->inited != 1) /* check handle initialization */
1564 {
1565 return 3; /* return error */
1566 }
1567 if (idle_frame_rate > 0x1F) /* check idle_frame_rate */
1568 {
1569 handle->debug_print("st7789: idle_frame_rate > 0x1F.\n"); /* idle_frame_rate > 0x1F */
1570
1571 return 4; /* return error */
1572 }
1573 if (partial_frame_rate > 0x1F) /* check partial_frame_rate */
1574 {
1575 handle->debug_print("st7789: partial_frame_rate > 0x1F.\n"); /* partial_frame_rate > 0x1F */
1576
1577 return 5; /* return error */
1578 }
1579
1580 if (a_st7789_write_byte(handle, ST7789_CMD_FRCTRL1, ST7789_CMD) != 0) /* write frame rate control 1 command */
1581 {
1582 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1583
1584 return 1; /* return error */
1585 }
1586 buf[0] = (separate_fr_control << 4) | (div_control << 0); /* set param1 */
1587 buf[1] = (idle_mode << 5) | ((idle_frame_rate & 0x1F) << 0); /* set param2 */
1588 buf[2] = (partial_mode << 5) | ((partial_frame_rate & 0x1F) << 0); /* set param3 */
1589 if (a_st7789_write_bytes(handle, buf, 3, ST7789_DATA) != 0) /* write data */
1590 {
1591 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1592
1593 return 1; /* return error */
1594 }
1595
1596 return 0; /* success return 0 */
1597}
1598
1616{
1617 uint8_t reg;
1618
1619 if (handle == NULL) /* check handle */
1620 {
1621 return 2; /* return error */
1622 }
1623 if (handle->inited != 1) /* check handle initialization */
1624 {
1625 return 3; /* return error */
1626 }
1627
1628 if (a_st7789_write_byte(handle, ST7789_CMD_PARCTRL, ST7789_CMD) != 0) /* write partial mode control command */
1629 {
1630 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1631
1632 return 1; /* return error */
1633 }
1634 reg = (level << 7) | (mode << 4) | (frequency << 0); /* set param */
1635 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
1636 {
1637 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1638
1639 return 1; /* return error */
1640 }
1641
1642 return 0; /* success return 0 */
1643}
1644
1658{
1659 uint8_t reg;
1660
1661 if (handle == NULL) /* check handle */
1662 {
1663 return 2; /* return error */
1664 }
1665 if (handle->inited != 1) /* check handle initialization */
1666 {
1667 return 3; /* return error */
1668 }
1669
1670 if (a_st7789_write_byte(handle, ST7789_CMD_GCTRL, ST7789_CMD) != 0) /* write gate control command */
1671 {
1672 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1673
1674 return 1; /* return error */
1675 }
1676 reg = (vghs << 4) | (vgls << 0); /* set param */
1677 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
1678 {
1679 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1680
1681 return 1; /* return error */
1682 }
1683
1684 return 0; /* success return 0 */
1685}
1686
1703 uint8_t gate_on_timing_adjustment,
1704 uint8_t gate_off_timing_adjustment_rgb,
1705 uint8_t gate_off_timing_adjustment)
1706{
1707 uint8_t buf[4];
1708
1709 if (handle == NULL) /* check handle */
1710 {
1711 return 2; /* return error */
1712 }
1713 if (handle->inited != 1) /* check handle initialization */
1714 {
1715 return 3; /* return error */
1716 }
1717 if (gate_on_timing_adjustment > 0x3F) /* check gate_on_timing_adjustment */
1718 {
1719 handle->debug_print("st7789: gate_on_timing_adjustment > 0x3F.\n"); /* gate_on_timing_adjustment > 0x3F */
1720
1721 return 4; /* return error */
1722 }
1723 if (gate_off_timing_adjustment_rgb > 0xF) /* check gate_off_timing_adjustment_rgb */
1724 {
1725 handle->debug_print("st7789: gate_off_timing_adjustment_rgb > 0xF.\n"); /* gate_off_timing_adjustment_rgb > 0xF */
1726
1727 return 5; /* return error */
1728 }
1729 if (gate_off_timing_adjustment > 0xF) /* check gate_off_timing_adjustment */
1730 {
1731 handle->debug_print("st7789: gate_off_timing_adjustment > 0xF.\n"); /* gate_off_timing_adjustment > 0xF */
1732
1733 return 6; /* return error */
1734 }
1735
1736 if (a_st7789_write_byte(handle, ST7789_CMD_GTADJ, ST7789_CMD) != 0) /* write gate on timing adjustment command */
1737 {
1738 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1739
1740 return 1; /* return error */
1741 }
1742 buf[0] = 0x2A; /* set param1 */
1743 buf[1] = 0x2B; /* set param2 */
1744 buf[2] = gate_on_timing_adjustment & 0x3F; /* set param3 */
1745 buf[3] = ((gate_off_timing_adjustment_rgb & 0xF) << 4) |
1746 ((gate_off_timing_adjustment & 0xF) << 0); /* set param4 */
1747 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
1748 {
1749 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1750
1751 return 1; /* return error */
1752 }
1753
1754 return 0; /* success return 0 */
1755}
1756
1769{
1770 uint8_t reg;
1771
1772 if (handle == NULL) /* check handle */
1773 {
1774 return 2; /* return error */
1775 }
1776 if (handle->inited != 1) /* check handle initialization */
1777 {
1778 return 3; /* return error */
1779 }
1780
1781 if (a_st7789_write_byte(handle, ST7789_CMD_DGMEN, ST7789_CMD) != 0) /* write digital gamma enable command */
1782 {
1783 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1784
1785 return 1; /* return error */
1786 }
1787 reg = enable << 2; /* set param */
1788 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
1789 {
1790 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1791
1792 return 1; /* return error */
1793 }
1794
1795 return 0; /* success return 0 */
1796}
1797
1810uint8_t st7789_set_vcoms(st7789_handle_t *handle, uint8_t vcoms)
1811{
1812 uint8_t reg;
1813
1814 if (handle == NULL) /* check handle */
1815 {
1816 return 2; /* return error */
1817 }
1818 if (handle->inited != 1) /* check handle initialization */
1819 {
1820 return 3; /* return error */
1821 }
1822 if (vcoms > 0x3F) /* check vcoms */
1823 {
1824 handle->debug_print("st7789: vcoms > 0x3F.\n"); /* vcoms > 0x3F */
1825
1826 return 4; /* return error */
1827 }
1828
1829 if (a_st7789_write_byte(handle, ST7789_CMD_VCOMS, ST7789_CMD) != 0) /* write vcoms setting command */
1830 {
1831 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1832
1833 return 1; /* return error */
1834 }
1835 reg = vcoms & 0x3F; /* set param */
1836 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
1837 {
1838 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1839
1840 return 1; /* return error */
1841 }
1842
1843 return 0; /* success return 0 */
1844}
1845
1857uint8_t st7789_vcom_convert_to_register(st7789_handle_t *handle, float v, uint8_t *reg)
1858{
1859 if (handle == NULL) /* check handle */
1860 {
1861 return 2; /* return error */
1862 }
1863 if (handle->inited != 1) /* check handle initialization */
1864 {
1865 return 3; /* return error */
1866 }
1867
1868 *reg = (uint8_t)((v - 0.1f) / 0.025f); /* convert real data to register data */
1869
1870 return 0; /* success return 0 */
1871}
1872
1884uint8_t st7789_vcom_convert_to_data(st7789_handle_t *handle, uint8_t reg, float *v)
1885{
1886 if (handle == NULL) /* check handle */
1887 {
1888 return 2; /* return error */
1889 }
1890 if (handle->inited != 1) /* check handle initialization */
1891 {
1892 return 3; /* return error */
1893 }
1894
1895 *v = (uint8_t)((float)(reg) * 0.025f + 0.1f); /* convert raw data to real data */
1896
1897 return 0; /* success return 0 */
1898}
1899
1918 st7789_bool_t xmy,
1919 st7789_bool_t xbgr,
1920 st7789_bool_t xinv,
1921 st7789_bool_t xmx,
1922 st7789_bool_t xmh,
1923 st7789_bool_t xmv,
1924 st7789_bool_t xgs)
1925{
1926 uint8_t reg;
1927
1928 if (handle == NULL) /* check handle */
1929 {
1930 return 2; /* return error */
1931 }
1932 if (handle->inited != 1) /* check handle initialization */
1933 {
1934 return 3; /* return error */
1935 }
1936
1937 if (a_st7789_write_byte(handle, ST7789_CMD_LCMCTRL, ST7789_CMD) != 0) /* write lcm control command */
1938 {
1939 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1940
1941 return 1; /* return error */
1942 }
1943 reg = (xmy << 6) | (xbgr << 5) | (xinv << 4) | (xmx << 3) |
1944 (xmh << 2) | (xmv << 1) | (xgs << 0); /* set param */
1945 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
1946 {
1947 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1948
1949 return 1; /* return error */
1950 }
1951
1952 return 0; /* success return 0 */
1953}
1954
1966uint8_t st7789_set_id_code_setting(st7789_handle_t *handle, uint8_t id[3])
1967{
1968 if (handle == NULL) /* check handle */
1969 {
1970 return 2; /* return error */
1971 }
1972 if (handle->inited != 1) /* check handle initialization */
1973 {
1974 return 3; /* return error */
1975 }
1976
1977 if (a_st7789_write_byte(handle, ST7789_CMD_IDSET, ST7789_CMD) != 0) /* write id setting command */
1978 {
1979 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
1980
1981 return 1; /* return error */
1982 }
1983 if (a_st7789_write_bytes(handle, id, 3, ST7789_DATA) != 0) /* write data */
1984 {
1985 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
1986
1987 return 1; /* return error */
1988 }
1989
1990 return 0; /* success return 0 */
1991}
1992
2005{
2006 uint8_t buf[2];
2007
2008 if (handle == NULL) /* check handle */
2009 {
2010 return 2; /* return error */
2011 }
2012 if (handle->inited != 1) /* check handle initialization */
2013 {
2014 return 3; /* return error */
2015 }
2016
2017 if (a_st7789_write_byte(handle, ST7789_CMD_VDVVRHEN, ST7789_CMD) != 0) /* write vdv and vrh command enable command */
2018 {
2019 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2020
2021 return 1; /* return error */
2022 }
2023 buf[0] = from; /* set param1 */
2024 buf[1] = 0xFF; /* set param2 */
2025 if (a_st7789_write_bytes(handle, buf, 2, ST7789_DATA) != 0) /* write data */
2026 {
2027 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2028
2029 return 1; /* return error */
2030 }
2031
2032 return 0; /* success return 0 */
2033}
2034
2047uint8_t st7789_set_vrhs(st7789_handle_t *handle, uint8_t vrhs)
2048{
2049 uint8_t reg;
2050
2051 if (handle == NULL) /* check handle */
2052 {
2053 return 2; /* return error */
2054 }
2055 if (handle->inited != 1) /* check handle initialization */
2056 {
2057 return 3; /* return error */
2058 }
2059 if (vrhs > 0x27) /* check vrhs */
2060 {
2061 handle->debug_print("st7789: vrhs > 0x27.\n"); /* vrhs > 0x27 */
2062
2063 return 4; /* return error */
2064 }
2065
2066 if (a_st7789_write_byte(handle, ST7789_CMD_VRHS, ST7789_CMD) != 0) /* write vrh set command */
2067 {
2068 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2069
2070 return 1; /* return error */
2071 }
2072 reg = vrhs & 0x3F; /* set param */
2073 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
2074 {
2075 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2076
2077 return 1; /* return error */
2078 }
2079
2080 return 0; /* success return 0 */
2081}
2082
2094uint8_t st7789_vrhs_convert_to_register(st7789_handle_t *handle, float v, uint8_t *reg)
2095{
2096 if (handle == NULL) /* check handle */
2097 {
2098 return 2; /* return error */
2099 }
2100 if (handle->inited != 1) /* check handle initialization */
2101 {
2102 return 3; /* return error */
2103 }
2104
2105 *reg = (uint8_t)((v - 3.55f) / 0.05f); /* convert real data to register data */
2106
2107 return 0; /* success return 0 */
2108}
2109
2121uint8_t st7789_vrhs_convert_to_data(st7789_handle_t *handle, uint8_t reg, float *v)
2122{
2123 if (handle == NULL) /* check handle */
2124 {
2125 return 2; /* return error */
2126 }
2127 if (handle->inited != 1) /* check handle initialization */
2128 {
2129 return 3; /* return error */
2130 }
2131
2132 *v = (uint8_t)((float)(reg) * 0.05f + 3.55f); /* convert raw data to real data */
2133
2134 return 0; /* success return 0 */
2135}
2136
2149uint8_t st7789_set_vdv(st7789_handle_t *handle, uint8_t vdv)
2150{
2151 uint8_t reg;
2152
2153 if (handle == NULL) /* check handle */
2154 {
2155 return 2; /* return error */
2156 }
2157 if (handle->inited != 1) /* check handle initialization */
2158 {
2159 return 3; /* return error */
2160 }
2161 if (vdv > 0x3F) /* check vdv */
2162 {
2163 handle->debug_print("st7789: vdv > 0x3F.\n"); /* vdv > 0x3F */
2164
2165 return 4; /* return error */
2166 }
2167
2168 if (a_st7789_write_byte(handle, ST7789_CMD_VDVSET, ST7789_CMD) != 0) /* write vdv setting command */
2169 {
2170 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2171
2172 return 1; /* return error */
2173 }
2174 reg = vdv & 0x3F; /* set param */
2175 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
2176 {
2177 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2178
2179 return 1; /* return error */
2180 }
2181
2182 return 0; /* success return 0 */
2183}
2184
2196uint8_t st7789_vdv_convert_to_register(st7789_handle_t *handle, float v, uint8_t *reg)
2197{
2198 if (handle == NULL) /* check handle */
2199 {
2200 return 2; /* return error */
2201 }
2202 if (handle->inited != 1) /* check handle initialization */
2203 {
2204 return 3; /* return error */
2205 }
2206
2207 *reg = (uint8_t)((v + 0.8f) / 0.025f); /* convert real data to register data */
2208
2209 return 0; /* success return 0 */
2210}
2211
2223uint8_t st7789_vdv_convert_to_data(st7789_handle_t *handle, uint8_t reg, float *v)
2224{
2225 if (handle == NULL) /* check handle */
2226 {
2227 return 2; /* return error */
2228 }
2229 if (handle->inited != 1) /* check handle initialization */
2230 {
2231 return 3; /* return error */
2232 }
2233
2234 *v = (uint8_t)((float)(reg) * 0.025f - 0.8f); /* convert raw data to real data */
2235
2236 return 0; /* success return 0 */
2237}
2238
2251uint8_t st7789_set_vcoms_offset(st7789_handle_t *handle, uint8_t offset)
2252{
2253 uint8_t reg;
2254
2255 if (handle == NULL) /* check handle */
2256 {
2257 return 2; /* return error */
2258 }
2259 if (handle->inited != 1) /* check handle initialization */
2260 {
2261 return 3; /* return error */
2262 }
2263 if (offset > 0x3F) /* check offset */
2264 {
2265 handle->debug_print("st7789: offset > 0x3F.\n"); /* offset > 0x3F */
2266
2267 return 4; /* return error */
2268 }
2269
2270 if (a_st7789_write_byte(handle, ST7789_CMD_VCMOFSET, ST7789_CMD) != 0) /* write vcoms offset set command */
2271 {
2272 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2273
2274 return 1; /* return error */
2275 }
2276 reg = offset & 0x3F; /* set param */
2277 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
2278 {
2279 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2280
2281 return 1; /* return error */
2282 }
2283
2284 return 0; /* success return 0 */
2285}
2286
2298uint8_t st7789_vcoms_offset_convert_to_register(st7789_handle_t *handle, float v, uint8_t *reg)
2299{
2300 if (handle == NULL) /* check handle */
2301 {
2302 return 2; /* return error */
2303 }
2304 if (handle->inited != 1) /* check handle initialization */
2305 {
2306 return 3; /* return error */
2307 }
2308
2309 *reg = (uint8_t)((v + 0.8f) / 0.025f); /* convert real data to register data */
2310
2311 return 0; /* success return 0 */
2312}
2313
2325uint8_t st7789_vcoms_offset_convert_to_data(st7789_handle_t *handle, uint8_t reg, float *v)
2326{
2327 if (handle == NULL) /* check handle */
2328 {
2329 return 2; /* return error */
2330 }
2331 if (handle->inited != 1) /* check handle initialization */
2332 {
2333 return 3; /* return error */
2334 }
2335
2336 *v = (uint8_t)((float)(reg) * 0.025f - 0.8f); /* convert raw data to real data */
2337
2338 return 0; /* success return 0 */
2339}
2340
2354{
2355 uint8_t reg;
2356
2357 if (handle == NULL) /* check handle */
2358 {
2359 return 2; /* return error */
2360 }
2361 if (handle->inited != 1) /* check handle initialization */
2362 {
2363 return 3; /* return error */
2364 }
2365
2366 if (a_st7789_write_byte(handle, ST7789_CMD_FRCTR2, ST7789_CMD) != 0) /* write fr control 2 command */
2367 {
2368 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2369
2370 return 1; /* return error */
2371 }
2372 reg = (selection << 5) | (rate << 0); /* set param */
2373 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
2374 {
2375 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2376
2377 return 1; /* return error */
2378 }
2379
2380 return 0; /* success return 0 */
2381}
2382
2398 st7789_bool_t led_on,
2399 st7789_bool_t led_pwm_init,
2400 st7789_bool_t led_pwm_fix,
2401 st7789_bool_t led_pwm_polarity)
2402{
2403 uint8_t reg;
2404
2405 if (handle == NULL) /* check handle */
2406 {
2407 return 2; /* return error */
2408 }
2409 if (handle->inited != 1) /* check handle initialization */
2410 {
2411 return 3; /* return error */
2412 }
2413
2414 if (a_st7789_write_byte(handle, ST7789_CMD_CABCCTRL, ST7789_CMD) != 0) /* write cabc control command */
2415 {
2416 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2417
2418 return 1; /* return error */
2419 }
2420 reg = (led_on << 3) | (led_pwm_init << 2) |
2421 (led_pwm_fix << 1) | (led_pwm_polarity << 0); /* set param */
2422 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
2423 {
2424 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2425
2426 return 1; /* return error */
2427 }
2428
2429 return 0; /* success return 0 */
2430}
2431
2444{
2445 uint8_t reg;
2446
2447 if (handle == NULL) /* check handle */
2448 {
2449 return 2; /* return error */
2450 }
2451 if (handle->inited != 1) /* check handle initialization */
2452 {
2453 return 3; /* return error */
2454 }
2455
2456 if (a_st7789_write_byte(handle, ST7789_CMD_PWMFRSEL, ST7789_CMD) != 0) /* write pwm frequency selection command */
2457 {
2458 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2459
2460 return 1; /* return error */
2461 }
2462 reg = frequency; /* set param */
2463 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
2464 {
2465 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2466
2467 return 1; /* return error */
2468 }
2469
2470 return 0; /* success return 0 */
2471}
2472
2487{
2488 uint8_t buf[2];
2489
2490 if (handle == NULL) /* check handle */
2491 {
2492 return 2; /* return error */
2493 }
2494 if (handle->inited != 1) /* check handle initialization */
2495 {
2496 return 3; /* return error */
2497 }
2498
2499 if (a_st7789_write_byte(handle, ST7789_CMD_PWCTRL1, ST7789_CMD) != 0) /* write power control 1 command */
2500 {
2501 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2502
2503 return 1; /* return error */
2504 }
2505 buf[0] = 0xA4; /* set param 1 */
2506 buf[1] = (avdd << 6) | (avcl << 4) | (vds << 0); /* set param 2 */
2507 if (a_st7789_write_bytes(handle, buf, 2, ST7789_DATA) != 0) /* write data */
2508 {
2509 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2510
2511 return 1; /* return error */
2512 }
2513
2514 return 0; /* success return 0 */
2515}
2516
2528{
2529 uint8_t reg;
2530
2531 if (handle == NULL) /* check handle */
2532 {
2533 return 2; /* return error */
2534 }
2535 if (handle->inited != 1) /* check handle initialization */
2536 {
2537 return 3; /* return error */
2538 }
2539
2540 if (a_st7789_write_byte(handle, ST7789_CMD_VAPVANEN, ST7789_CMD) != 0) /* write enable vap/van signal output command */
2541 {
2542 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2543
2544 return 1; /* return error */
2545 }
2546 reg = 0x4C; /* set param */
2547 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
2548 {
2549 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2550
2551 return 1; /* return error */
2552 }
2553
2554 return 0; /* success return 0 */
2555}
2556
2569{
2570 uint8_t buf[4];
2571
2572 if (handle == NULL) /* check handle */
2573 {
2574 return 2; /* return error */
2575 }
2576 if (handle->inited != 1) /* check handle initialization */
2577 {
2578 return 3; /* return error */
2579 }
2580
2581 if (a_st7789_write_byte(handle, ST7789_CMD_CMD2EN, ST7789_CMD) != 0) /* write command 2 enable command */
2582 {
2583 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2584
2585 return 1; /* return error */
2586 }
2587 buf[0] = 0x5A; /* set param 1 */
2588 buf[1] = 0x69; /* set param 2 */
2589 buf[2] = 0x02; /* set param 3 */
2590 buf[3] = enable; /* set param 4 */
2591 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
2592 {
2593 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2594
2595 return 1; /* return error */
2596 }
2597
2598 return 0; /* success return 0 */
2599}
2600
2613{
2614 if (handle == NULL) /* check handle */
2615 {
2616 return 2; /* return error */
2617 }
2618 if (handle->inited != 1) /* check handle initialization */
2619 {
2620 return 3; /* return error */
2621 }
2622
2623 if (a_st7789_write_byte(handle, ST7789_CMD_PVGAMCTRL, ST7789_CMD) != 0) /* write positive voltage gamma control command */
2624 {
2625 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2626
2627 return 1; /* return error */
2628 }
2629 if (a_st7789_write_bytes(handle, param, 14, ST7789_DATA) != 0) /* write data */
2630 {
2631 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2632
2633 return 1; /* return error */
2634 }
2635
2636 return 0; /* success return 0 */
2637}
2638
2651{
2652 if (handle == NULL) /* check handle */
2653 {
2654 return 2; /* return error */
2655 }
2656 if (handle->inited != 1) /* check handle initialization */
2657 {
2658 return 3; /* return error */
2659 }
2660
2661 if (a_st7789_write_byte(handle, ST7789_CMD_NVGAMCTRL, ST7789_CMD) != 0) /* write negative voltage gamma control command */
2662 {
2663 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2664
2665 return 1; /* return error */
2666 }
2667 if (a_st7789_write_bytes(handle, param, 14, ST7789_DATA) != 0) /* write data */
2668 {
2669 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2670
2671 return 1; /* return error */
2672 }
2673
2674 return 0; /* success return 0 */
2675}
2676
2689{
2690 if (handle == NULL) /* check handle */
2691 {
2692 return 2; /* return error */
2693 }
2694 if (handle->inited != 1) /* check handle initialization */
2695 {
2696 return 3; /* return error */
2697 }
2698
2699 if (a_st7789_write_byte(handle, ST7789_CMD_DGMLUTR, ST7789_CMD) != 0) /* write digital gamma look-up table for red command */
2700 {
2701 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2702
2703 return 1; /* return error */
2704 }
2705 if (a_st7789_write_bytes(handle, param, 64, ST7789_DATA) != 0) /* write data */
2706 {
2707 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2708
2709 return 1; /* return error */
2710 }
2711
2712 return 0; /* success return 0 */
2713}
2714
2727{
2728 if (handle == NULL) /* check handle */
2729 {
2730 return 2; /* return error */
2731 }
2732 if (handle->inited != 1) /* check handle initialization */
2733 {
2734 return 3; /* return error */
2735 }
2736
2737 if (a_st7789_write_byte(handle, ST7789_CMD_DGMLUTB, ST7789_CMD) != 0) /* write digital gamma look-up table for blue command */
2738 {
2739 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2740
2741 return 1; /* return error */
2742 }
2743 if (a_st7789_write_bytes(handle, param, 64, ST7789_DATA) != 0) /* write data */
2744 {
2745 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2746
2747 return 1; /* return error */
2748 }
2749
2750 return 0; /* success return 0 */
2751}
2752
2771 uint8_t gate_line_number,
2772 uint8_t first_scan_line_number,
2775{
2776 uint8_t buf[3];
2777
2778 if (handle == NULL) /* check handle */
2779 {
2780 return 2; /* return error */
2781 }
2782 if (handle->inited != 1) /* check handle initialization */
2783 {
2784 return 3; /* return error */
2785 }
2786 if (gate_line_number > 0x3F) /* check gate_line_number */
2787 {
2788 handle->debug_print("st7789: gate_line_number > 0x3F.\n"); /* gate_line_number > 0x3F */
2789
2790 return 4; /* return error */
2791 }
2792 if (first_scan_line_number > 0x3F) /* check first_scan_line_number */
2793 {
2794 handle->debug_print("st7789: first_scan_line_number > 0x3F.\n"); /* first_scan_line_number > 0x3F */
2795
2796 return 5; /* return error */
2797 }
2798
2799 if (a_st7789_write_byte(handle, ST7789_CMD_GATECTRL, ST7789_CMD) != 0) /* write gate control command */
2800 {
2801 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2802
2803 return 1; /* return error */
2804 }
2805 buf[0] = gate_line_number; /* set param 1 */
2806 buf[1] = first_scan_line_number; /* set param 2 */
2807 buf[2] = 0x10 | (mode << 2) | (direction << 0); /* set param 3 */
2808 if (a_st7789_write_bytes(handle, buf, 3, ST7789_DATA) != 0) /* write data */
2809 {
2810 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2811
2812 return 1; /* return error */
2813 }
2814
2815 return 0; /* success return 0 */
2816}
2817
2829uint8_t st7789_gate_line_convert_to_register(st7789_handle_t *handle, uint16_t l, uint8_t *reg)
2830{
2831 if (handle == NULL) /* check handle */
2832 {
2833 return 2; /* return error */
2834 }
2835 if (handle->inited != 1) /* check handle initialization */
2836 {
2837 return 3; /* return error */
2838 }
2839
2840 *reg = (uint8_t)((l / 8) - 1); /* convert real data to register data */
2841
2842 return 0; /* success return 0 */
2843}
2844
2856uint8_t st7789_gate_line_convert_to_data(st7789_handle_t *handle, uint8_t reg, uint16_t *l)
2857{
2858 if (handle == NULL) /* check handle */
2859 {
2860 return 2; /* return error */
2861 }
2862 if (handle->inited != 1) /* check handle initialization */
2863 {
2864 return 3; /* return error */
2865 }
2866
2867 *l = (uint8_t)(reg * 8 + 8); /* convert raw data to real data */
2868
2869 return 0; /* success return 0 */
2870}
2871
2884uint8_t st7789_set_spi2_enable(st7789_handle_t *handle, st7789_bool_t date_lane, st7789_bool_t command_table_2)
2885{
2886 uint8_t reg;
2887
2888 if (handle == NULL) /* check handle */
2889 {
2890 return 2; /* return error */
2891 }
2892 if (handle->inited != 1) /* check handle initialization */
2893 {
2894 return 3; /* return error */
2895 }
2896
2897 if (a_st7789_write_byte(handle, ST7789_CMD_SPI2EN, ST7789_CMD) != 0) /* write spi2 command */
2898 {
2899 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2900
2901 return 1; /* return error */
2902 }
2903 reg = (date_lane << 4) | (command_table_2 << 0); /* set param */
2904 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
2905 {
2906 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2907
2908 return 1; /* return error */
2909 }
2910
2911 return 0; /* success return 0 */
2912}
2913
2927{
2928 uint8_t reg;
2929
2930 if (handle == NULL) /* check handle */
2931 {
2932 return 2; /* return error */
2933 }
2934 if (handle->inited != 1) /* check handle initialization */
2935 {
2936 return 3; /* return error */
2937 }
2938
2939 if (a_st7789_write_byte(handle, ST7789_CMD_PWCTRL2, ST7789_CMD) != 0) /* write power control 2 command */
2940 {
2941 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
2942
2943 return 1; /* return error */
2944 }
2945 reg = (sbclk << 4) | (stp14ck << 0); /* set param */
2946 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
2947 {
2948 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
2949
2950 return 1; /* return error */
2951 }
2952
2953 return 0; /* success return 0 */
2954}
2955
2973 uint8_t source_equalize_time,
2974 uint8_t source_pre_drive_time,
2975 uint8_t gate_equalize_time)
2976{
2977 uint8_t buf[3];
2978
2979 if (handle == NULL) /* check handle */
2980 {
2981 return 2; /* return error */
2982 }
2983 if (handle->inited != 1) /* check handle initialization */
2984 {
2985 return 3; /* return error */
2986 }
2987 if (source_equalize_time > 0x1F) /* check source_equalize_time */
2988 {
2989 handle->debug_print("st7789: source_equalize_time > 0x1F.\n"); /* source_equalize_time > 0x1F */
2990
2991 return 4; /* return error */
2992 }
2993 if (source_pre_drive_time > 0x1F) /* check source_pre_drive_time */
2994 {
2995 handle->debug_print("st7789: source_pre_drive_time > 0x1F.\n"); /* source_pre_drive_time > 0x1F */
2996
2997 return 5; /* return error */
2998 }
2999 if (gate_equalize_time > 0xF) /* check gate_equalize_time */
3000 {
3001 handle->debug_print("st7789: gate_equalize_time > 0xF.\n"); /* gate_equalize_time > 0xF */
3002
3003 return 6; /* return error */
3004 }
3005
3006 if (a_st7789_write_byte(handle, ST7789_CMD_EQCTRL, ST7789_CMD) != 0) /* write equalize time control command */
3007 {
3008 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3009
3010 return 1; /* return error */
3011 }
3012 buf[0] = source_equalize_time; /* set param 1 */
3013 buf[1] = source_pre_drive_time; /* set param 2 */
3014 buf[2] = gate_equalize_time; /* set param 3 */
3015 if (a_st7789_write_bytes(handle, buf, 3, ST7789_DATA) != 0) /* write data */
3016 {
3017 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3018
3019 return 1; /* return error */
3020 }
3021
3022 return 0; /* success return 0 */
3023}
3024
3036{
3037 uint8_t reg;
3038
3039 if (handle == NULL) /* check handle */
3040 {
3041 return 2; /* return error */
3042 }
3043 if (handle->inited != 1) /* check handle initialization */
3044 {
3045 return 3; /* return error */
3046 }
3047
3048 if (a_st7789_write_byte(handle, ST7789_CMD_PROMCTRL, ST7789_CMD) != 0) /* write program control command */
3049 {
3050 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3051
3052 return 1; /* return error */
3053 }
3054 reg = 0x01; /* set param */
3055 if (a_st7789_write_byte(handle, reg, ST7789_DATA) != 0) /* write data */
3056 {
3057 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3058
3059 return 1; /* return error */
3060 }
3061
3062 return 0; /* success return 0 */
3063}
3064
3077{
3078 uint8_t buf[4];
3079
3080 if (handle == NULL) /* check handle */
3081 {
3082 return 2; /* return error */
3083 }
3084 if (handle->inited != 1) /* check handle initialization */
3085 {
3086 return 3; /* return error */
3087 }
3088
3089 if (a_st7789_write_byte(handle, ST7789_CMD_PROMEN, ST7789_CMD) != 0) /* write program mode enable command */
3090 {
3091 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3092
3093 return 1; /* return error */
3094 }
3095 buf[0] = 0x5A; /* set param 1 */
3096 buf[1] = 0x69; /* set param 2 */
3097 buf[2] = 0xEE; /* set param 3 */
3098 buf[3] = enable << 2; /* set param 4 */
3099 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
3100 {
3101 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3102
3103 return 1; /* return error */
3104 }
3105
3106 return 0; /* success return 0 */
3107}
3108
3121uint8_t st7789_set_nvm_setting(st7789_handle_t *handle, uint8_t addr, uint8_t data)
3122{
3123 uint8_t buf[2];
3124
3125 if (handle == NULL) /* check handle */
3126 {
3127 return 2; /* return error */
3128 }
3129 if (handle->inited != 1) /* check handle initialization */
3130 {
3131 return 3; /* return error */
3132 }
3133
3134 if (a_st7789_write_byte(handle, ST7789_CMD_NVMSET, ST7789_CMD) != 0) /* write nvm setting command */
3135 {
3136 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3137
3138 return 1; /* return error */
3139 }
3140 buf[0] = addr; /* set param 1 */
3141 buf[1] = data; /* set param 2 */
3142 if (a_st7789_write_bytes(handle, buf, 2, ST7789_DATA) != 0) /* write data */
3143 {
3144 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3145
3146 return 1; /* return error */
3147 }
3148
3149 return 0; /* success return 0 */
3150}
3151
3163{
3164 uint8_t buf[2];
3165
3166 if (handle == NULL) /* check handle */
3167 {
3168 return 2; /* return error */
3169 }
3170 if (handle->inited != 1) /* check handle initialization */
3171 {
3172 return 3; /* return error */
3173 }
3174
3175 if (a_st7789_write_byte(handle, ST7789_CMD_PROMACT, ST7789_CMD) != 0) /* write program action command */
3176 {
3177 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3178
3179 return 1; /* return error */
3180 }
3181 buf[0] = 0x29; /* set param 1 */
3182 buf[1] = 0xA5; /* set param 2 */
3183 if (a_st7789_write_bytes(handle, buf, 2, ST7789_DATA) != 0) /* write data */
3184 {
3185 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3186
3187 return 1; /* return error */
3188 }
3189
3190 return 0; /* success return 0 */
3191}
3192
3206{
3207 if (handle == NULL) /* check handle */
3208 {
3209 return 2; /* return error */
3210 }
3211 if (handle->debug_print == NULL) /* check debug_print */
3212 {
3213 return 3; /* return error */
3214 }
3215 if (handle->spi_init == NULL) /* check spi_init */
3216 {
3217 handle->debug_print("st7789: spi_init is null.\n"); /* spi_init is null */
3218
3219 return 3; /* return error */
3220 }
3221 if (handle->spi_deinit == NULL) /* check spi_deinit */
3222 {
3223 handle->debug_print("st7789: spi_deinit is null.\n"); /* spi_deinit is null */
3224
3225 return 3; /* return error */
3226 }
3227 if (handle->spi_write_cmd == NULL) /* check spi_write_cmd */
3228 {
3229 handle->debug_print("st7789: spi_write_cmd is null.\n"); /* spi_init is null */
3230
3231 return 3; /* return error */
3232 }
3233 if (handle->cmd_data_gpio_init == NULL) /* check cmd_data_gpio_init */
3234 {
3235 handle->debug_print("st7789: cmd_data_gpio_init is null.\n"); /* cmd_data_gpio_init is null */
3236
3237 return 3; /* return error */
3238 }
3239 if (handle->cmd_data_gpio_deinit == NULL) /* check cmd_data_gpio_deinit */
3240 {
3241 handle->debug_print("st7789: cmd_data_gpio_deinit is null.\n"); /* cmd_data_gpio_deinit is null */
3242
3243 return 3; /* return error */
3244 }
3245 if (handle->cmd_data_gpio_write == NULL) /* check cmd_data_gpio_write */
3246 {
3247 handle->debug_print("st7789: cmd_data_gpio_write is null.\n"); /* cmd_data_gpio_write is null */
3248
3249 return 3; /* return error */
3250 }
3251 if (handle->reset_gpio_init == NULL) /* check reset_gpio_init */
3252 {
3253 handle->debug_print("st7789: reset_gpio_init is null.\n"); /* reset_gpio_init is null */
3254
3255 return 3; /* return error */
3256 }
3257 if (handle->reset_gpio_deinit == NULL) /* check reset_gpio_deinit */
3258 {
3259 handle->debug_print("st7789: reset_gpio_deinit is null.\n"); /* reset_gpio_deinit is null */
3260
3261 return 3; /* return error */
3262 }
3263 if (handle->reset_gpio_write == NULL) /* check reset_gpio_write */
3264 {
3265 handle->debug_print("st7789: reset_gpio_write is null.\n"); /* reset_gpio_write is null */
3266
3267 return 3; /* return error */
3268 }
3269 if (handle->delay_ms == NULL) /* check delay_ms */
3270 {
3271 handle->debug_print("st7789: delay_ms is null.\n"); /* delay_ms is null */
3272
3273 return 3; /* return error */
3274 }
3275
3276 if (handle->cmd_data_gpio_init() != 0) /* check cmd_data_gpio_init */
3277 {
3278 handle->debug_print("st7789: cmd data gpio init failed.\n"); /* cmd data gpio init failed */
3279
3280 return 5; /* return error */
3281 }
3282 if (handle->reset_gpio_init() != 0) /* reset gpio init */
3283 {
3284 handle->debug_print("st7789: reset gpio init failed.\n"); /* reset gpio init failed */
3285 (void)handle->cmd_data_gpio_deinit(); /* cmd_data_gpio_deinit */
3286
3287 return 4; /* return error */
3288 }
3289 if (handle->reset_gpio_write(0) != 0) /* write 0 */
3290 {
3291 handle->debug_print("st7789: reset gpio write failed.\n"); /* reset gpio write failed */
3292 (void)handle->cmd_data_gpio_deinit(); /* cmd_data_gpio_deinit */
3293 (void)handle->reset_gpio_deinit(); /* reset_gpio_deinit */
3294
3295 return 4; /* return error */
3296 }
3297 handle->delay_ms(25); /* over 10 us */
3298 if (handle->reset_gpio_write(1) != 0) /* write 1 */
3299 {
3300 handle->debug_print("st7789: reset gpio write failed.\n"); /* reset gpio write failed */
3301 (void)handle->cmd_data_gpio_deinit(); /* cmd_data_gpio_deinit */
3302 (void)handle->reset_gpio_deinit(); /* reset_gpio_deinit */
3303
3304 return 4; /* return error */
3305 }
3306 handle->delay_ms(125); /* over 120 ms */
3307 if (handle->spi_init() != 0) /* spi init */
3308 {
3309 handle->debug_print("st7789: spi init failed.\n"); /* spi init failed */
3310 (void)handle->cmd_data_gpio_deinit(); /* cmd_data_gpio_deinit */
3311 (void)handle->reset_gpio_deinit(); /* reset_gpio_deinit */
3312
3313 return 1; /* return error */
3314 }
3315 handle->inited = 1; /* flag inited */
3316
3317 return 0; /* success return 0 */
3318}
3319
3334{
3335 if (handle == NULL) /* check handle */
3336 {
3337 return 2; /* return error */
3338 }
3339 if (handle->inited != 1) /* check handle initialization */
3340 {
3341 return 3; /* return error */
3342 }
3343
3344 if (a_st7789_write_byte(handle, ST7789_CMD_SLPIN, ST7789_CMD) != 0) /* write sleep in command */
3345 {
3346 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3347
3348 return 1; /* return error */
3349 }
3350 if (handle->reset_gpio_deinit() != 0) /* reset gpio deinit */
3351 {
3352 handle->debug_print("st7789: reset gpio deinit failed.\n"); /* reset gpio deinit failed */
3353
3354 return 5; /* return error */
3355 }
3356 if (handle->cmd_data_gpio_deinit() != 0) /* cmd data gpio deinit */
3357 {
3358 handle->debug_print("st7789: cmd data gpio deinit failed.\n"); /* cmd data gpio deinit failed */
3359
3360 return 6; /* return error */
3361 }
3362 if (handle->spi_deinit() != 0) /* spi deinit */
3363 {
3364 handle->debug_print("st7789: spi deinit failed.\n"); /* spi deinit failed */
3365
3366 return 1; /* return error */
3367 }
3368 handle->inited = 0; /* flag close */
3369
3370 return 0; /* success return 0 */
3371}
3372
3384uint8_t st7789_set_column(st7789_handle_t *handle, uint16_t column)
3385{
3386 if (handle == NULL) /* check handle */
3387 {
3388 return 2; /* return error */
3389 }
3390 if (handle->inited != 1) /* check handle initialization */
3391 {
3392 return 3; /* return error */
3393 }
3394 if (column > 240) /* check column */
3395 {
3396 handle->debug_print("st7789: column > 240.\n"); /* column > 240 */
3397
3398 return 4; /* return error */
3399 }
3400
3401 handle->column = column; /* set column */
3402
3403 return 0; /* success return 0 */
3404}
3405
3417uint8_t st7789_set_row(st7789_handle_t *handle, uint16_t row)
3418{
3419 if (handle == NULL) /* check handle */
3420 {
3421 return 2; /* return error */
3422 }
3423 if (handle->inited != 1) /* check handle initialization */
3424 {
3425 return 3; /* return error */
3426 }
3427 if (row > 320) /* check column */
3428 {
3429 handle->debug_print("st7789: row > 320.\n"); /* row > 320 */
3430
3431 return 4; /* return error */
3432 }
3433
3434 handle->row = row; /* set row */
3435
3436 return 0; /* success return 0 */
3437}
3438
3451{
3452 uint8_t buf[4];
3453 uint32_t i;
3454 uint32_t m;
3455 uint32_t n;
3456
3457 if (handle == NULL) /* check handle */
3458 {
3459 return 2; /* return error */
3460 }
3461 if (handle->inited != 1) /* check handle initialization */
3462 {
3463 return 3; /* return error */
3464 }
3465
3466 if (a_st7789_write_byte(handle, ST7789_CMD_CASET, ST7789_CMD) != 0) /* write set column address command */
3467 {
3468 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3469
3470 return 1; /* return error */
3471 }
3472 buf[0] = (0x00 >> 8) & 0xFF; /* start address msb */
3473 buf[1] = (0x00 >> 0) & 0xFF; /* start address lsb */
3474 buf[2] = ((handle->column - 1) >> 8) & 0xFF; /* end address msb */
3475 buf[3] = ((handle->column - 1) >> 0) & 0xFF; /* end address lsb */
3476 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
3477 {
3478 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3479
3480 return 1; /* return error */
3481 }
3482
3483 if (a_st7789_write_byte(handle, ST7789_CMD_RASET, ST7789_CMD) != 0) /* write set row address command */
3484 {
3485 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3486
3487 return 1; /* return error */
3488 }
3489 buf[0] = (0x00 >> 8) & 0xFF; /* start address msb */
3490 buf[1] = (0x00 >> 0) & 0xFF; /* start address lsb */
3491 buf[2] = ((handle->row - 1) >> 8) & 0xFF; /* end address msb */
3492 buf[3] = ((handle->row - 1) >> 0) & 0xFF; /* end address lsb */
3493 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
3494 {
3495 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3496
3497 return 1; /* return error */
3498 }
3499
3500 if (a_st7789_write_byte(handle, ST7789_CMD_RAMWR, ST7789_CMD) != 0) /* write memory write command */
3501 {
3502 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3503
3504 return 1; /* return error */
3505 }
3506
3507 if ((handle->format & 0x03) == 0x03) /* rgb444 */
3508 {
3509 memset(handle->buf, 0x00, sizeof(uint8_t) * ST7789_BUFFER_SIZE); /* clear buffer */
3510 m = ((uint32_t)(handle->row) * handle->column * 3 / 2) /
3511 ST7789_BUFFER_SIZE; /* total times */
3512 n = ((uint32_t)(handle->row) * handle->column * 3 / 2) %
3513 ST7789_BUFFER_SIZE; /* the last */
3514 for (i = 0; i < m; i++)
3515 {
3516 if (a_st7789_write_bytes(handle, handle->buf,
3517 ST7789_BUFFER_SIZE, ST7789_DATA) != 0) /* write data */
3518 {
3519 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3520
3521 return 1; /* return error */
3522 }
3523 }
3524 if (n != 0) /* not end */
3525 {
3526 if (a_st7789_write_bytes(handle, handle->buf, n, ST7789_DATA) != 0) /* write data */
3527 {
3528 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3529
3530 return 1; /* return error */
3531 }
3532 }
3533 }
3534 else if ((handle->format & 0x05) == 0x05) /* rgb565 */
3535 {
3536 memset(handle->buf, 0x00, sizeof(uint8_t) * ST7789_BUFFER_SIZE); /* clear buffer */
3537 m = (uint32_t)(handle->row) * handle->column * 2 / ST7789_BUFFER_SIZE; /* total times */
3538 n = (uint32_t)(handle->row) * handle->column * 2 % ST7789_BUFFER_SIZE; /* the last */
3539 for (i = 0; i < m; i++)
3540 {
3541 if (a_st7789_write_bytes(handle, handle->buf,
3542 ST7789_BUFFER_SIZE, ST7789_DATA) != 0) /* write data */
3543 {
3544 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3545
3546 return 1; /* return error */
3547 }
3548 }
3549 if (n != 0) /* not end */
3550 {
3551 if (a_st7789_write_bytes(handle, handle->buf, n, ST7789_DATA) != 0) /* write data */
3552 {
3553 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3554
3555 return 1; /* return error */
3556 }
3557 }
3558 }
3559 else if ((handle->format & 0x06) == 0x06) /* rgb666 */
3560 {
3561 memset(handle->buf, 0x00, sizeof(uint8_t) * ST7789_BUFFER_SIZE); /* clear buffer */
3562 m = (uint32_t)(handle->row) * handle->column * 3 / ST7789_BUFFER_SIZE; /* total times */
3563 n = (uint32_t)(handle->row) * handle->column * 3 % ST7789_BUFFER_SIZE; /* the last */
3564 for (i = 0; i < m; i++)
3565 {
3566 if (a_st7789_write_bytes(handle, handle->buf,
3567 ST7789_BUFFER_SIZE, ST7789_DATA) != 0) /* write data */
3568 {
3569 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3570
3571 return 1; /* return error */
3572 }
3573 }
3574 if (n != 0) /* not end */
3575 {
3576 if (a_st7789_write_bytes(handle, handle->buf, n, ST7789_DATA) != 0) /* write data */
3577 {
3578 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3579
3580 return 1; /* return error */
3581 }
3582 }
3583 }
3584 else
3585 {
3586 handle->debug_print("st7789: format is invalid.\n"); /* format is invalid */
3587
3588 return 4; /* return error */
3589 }
3590
3591 return 0; /* success return 0 */
3592}
3593
3615uint8_t st7789_fill_rect(st7789_handle_t *handle, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint32_t color)
3616{
3617 uint8_t buf[4];
3618 uint32_t i;
3619 uint32_t m;
3620 uint32_t n;
3621
3622 if (handle == NULL) /* check handle */
3623 {
3624 return 2; /* return error */
3625 }
3626 if (handle->inited != 1) /* check handle initialization */
3627 {
3628 return 3; /* return error */
3629 }
3630 if (left > (handle->column - 1)) /* check left */
3631 {
3632 handle->debug_print("st7789: left is over column.\n"); /* left is over column */
3633
3634 return 4; /* return error */
3635 }
3636 if (right > (handle->column - 1)) /* check right */
3637 {
3638 handle->debug_print("st7789: right is over column.\n"); /* right is over column */
3639
3640 return 5; /* return error */
3641 }
3642 if (left >= right) /* check left and right */
3643 {
3644 handle->debug_print("st7789: left >= right.\n"); /* left >= right */
3645
3646 return 6; /* return error */
3647 }
3648 if (top > (handle->row - 1)) /* check top */
3649 {
3650 handle->debug_print("st7789: top is over row.\n"); /* top is over row */
3651
3652 return 7; /* return error */
3653 }
3654 if (bottom > (handle->row - 1)) /* check bottom */
3655 {
3656 handle->debug_print("st7789: bottom is over row.\n"); /* bottom is over row */
3657
3658 return 8; /* return error */
3659 }
3660 if (top >= bottom) /* check top and bottom */
3661 {
3662 handle->debug_print("st7789: top >= bottom.\n"); /* top >= bottom */
3663
3664 return 9; /* return error */
3665 }
3666
3667 if (a_st7789_write_byte(handle, ST7789_CMD_CASET, ST7789_CMD) != 0) /* write set column address command */
3668 {
3669 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3670
3671 return 1; /* return error */
3672 }
3673 buf[0] = (left >> 8) & 0xFF; /* start address msb */
3674 buf[1] = (left >> 0) & 0xFF; /* start address lsb */
3675 buf[2] = ((right) >> 8) & 0xFF; /* end address msb */
3676 buf[3] = ((right) >> 0) & 0xFF; /* end address lsb */
3677 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
3678 {
3679 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3680
3681 return 1; /* return error */
3682 }
3683
3684 if (a_st7789_write_byte(handle, ST7789_CMD_RASET, ST7789_CMD) != 0) /* write set row address command */
3685 {
3686 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3687
3688 return 1; /* return error */
3689 }
3690 buf[0] = (top >> 8) & 0xFF; /* start address msb */
3691 buf[1] = (top >> 0) & 0xFF; /* start address lsb */
3692 buf[2] = ((bottom) >> 8) & 0xFF; /* end address msb */
3693 buf[3] = ((bottom) >> 0) & 0xFF; /* end address lsb */
3694 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
3695 {
3696 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3697
3698 return 1; /* return error */
3699 }
3700
3701 if (a_st7789_write_byte(handle, ST7789_CMD_RAMWR, ST7789_CMD) != 0) /* write memory write command */
3702 {
3703 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3704
3705 return 1; /* return error */
3706 }
3707
3708 if ((handle->format & 0x03) == 0x03) /* rgb444 */
3709 {
3710 for (i = 0; i < ST7789_BUFFER_SIZE; i += 3) /* fill the buffer */
3711 {
3712 handle->buf[i] = (((color >> 8) & 0xF) << 4) |
3713 (((color >> 4) & 0xF) << 0); /* set the color */
3714 handle->buf[i + 1] = (((color >> 0) & 0xF) << 4) |
3715 (((color >> 8) & 0xF) << 0); /* set the color */
3716 handle->buf[i + 2] = (((color >> 4) & 0xF) << 4) |
3717 (((color >> 0) & 0xF) << 0); /* set the color */
3718 }
3719 m = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 3 / 2) /
3720 ST7789_BUFFER_SIZE; /* total times */
3721 n = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 3 / 2) %
3722 ST7789_BUFFER_SIZE; /* the last */
3723 for (i = 0; i < m; i++)
3724 {
3725 if (a_st7789_write_bytes(handle, handle->buf,
3726 ST7789_BUFFER_SIZE, ST7789_DATA) != 0) /* write data */
3727 {
3728 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3729
3730 return 1; /* return error */
3731 }
3732 }
3733 if (n != 0) /* not end */
3734 {
3735 if (a_st7789_write_bytes(handle, handle->buf, n, ST7789_DATA) != 0) /* write data */
3736 {
3737 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3738
3739 return 1; /* return error */
3740 }
3741 }
3742 }
3743 else if ((handle->format & 0x05) == 0x05) /* rgb565 */
3744 {
3745 for (i = 0; i < ST7789_BUFFER_SIZE; i += 2) /* fill the buffer */
3746 {
3747 handle->buf[i] = (color >> 8) & 0xFF; /* set the color */
3748 handle->buf[i + 1] = (color >> 0) & 0xFF; /* set the color */
3749 }
3750 m = (uint32_t)(right - left + 1) * (bottom - top + 1) * 2 /
3751 ST7789_BUFFER_SIZE; /* total times */
3752 n = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 2) %
3753 ST7789_BUFFER_SIZE; /* the last */
3754 for (i = 0; i < m; i++)
3755 {
3756 if (a_st7789_write_bytes(handle, handle->buf,
3757 ST7789_BUFFER_SIZE, ST7789_DATA) != 0) /* write data */
3758 {
3759 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3760
3761 return 1; /* return error */
3762 }
3763 }
3764 if (n != 0) /* not end */
3765 {
3766 if (a_st7789_write_bytes(handle, handle->buf, n, ST7789_DATA) != 0) /* write data */
3767 {
3768 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3769
3770 return 1; /* return error */
3771 }
3772 }
3773 }
3774 else if ((handle->format & 0x06) == 0x06) /* rgb666 */
3775 {
3776 for (i = 0; i < ST7789_BUFFER_SIZE; i += 3) /* fill the buffer */
3777 {
3778 handle->buf[i] = ((color >> 12) & 0x3F) << 2; /* set the color */
3779 handle->buf[i + 1] = ((color >> 6) & 0x3F) << 2; /* set the color */
3780 handle->buf[i + 2] = ((color >> 0) & 0x3F) << 2; /* set the color */
3781 }
3782 m = (uint32_t)(right - left + 1) * (bottom - top + 1) * 3 /
3783 ST7789_BUFFER_SIZE; /* total times */
3784 n = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 3) %
3785 ST7789_BUFFER_SIZE; /* the last */
3786 for (i = 0; i < m; i++)
3787 {
3788 if (a_st7789_write_bytes(handle, handle->buf,
3789 ST7789_BUFFER_SIZE, ST7789_DATA) != 0) /* write data */
3790 {
3791 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3792
3793 return 1; /* return error */
3794 }
3795 }
3796 if (n != 0) /* not end */
3797 {
3798 if (a_st7789_write_bytes(handle, handle->buf, n, ST7789_DATA) != 0) /* write data */
3799 {
3800 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3801
3802 return 1; /* return error */
3803 }
3804 }
3805 }
3806 else
3807 {
3808 handle->debug_print("st7789: format is invalid.\n"); /* format is invalid */
3809
3810 return 4; /* return error */
3811 }
3812
3813 return 0; /* success return 0 */
3814}
3815
3837uint8_t st7789_draw_picture_12bits(st7789_handle_t *handle, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint16_t *image)
3838{
3839 uint8_t buf[4];
3840 uint32_t i;
3841 uint32_t j;
3842 uint32_t m;
3843 uint32_t n;
3844 uint32_t point;
3845
3846 if (handle == NULL) /* check handle */
3847 {
3848 return 2; /* return error */
3849 }
3850 if (handle->inited != 1) /* check handle initialization */
3851 {
3852 return 3; /* return error */
3853 }
3854 if (left > (handle->column - 1)) /* check left */
3855 {
3856 handle->debug_print("st7789: left is over column.\n"); /* left is over column */
3857
3858 return 4; /* return error */
3859 }
3860 if (right > (handle->column - 1)) /* check right */
3861 {
3862 handle->debug_print("st7789: right is over column.\n"); /* right is over column */
3863
3864 return 5; /* return error */
3865 }
3866 if (left >= right) /* check left and right */
3867 {
3868 handle->debug_print("st7789: left >= right.\n"); /* left >= right */
3869
3870 return 6; /* return error */
3871 }
3872 if (top > (handle->row - 1)) /* check top */
3873 {
3874 handle->debug_print("st7789: top is over row.\n"); /* top is over row */
3875
3876 return 7; /* return error */
3877 }
3878 if (bottom > (handle->row - 1)) /* check bottom */
3879 {
3880 handle->debug_print("st7789: bottom is over row.\n"); /* bottom is over row */
3881
3882 return 8; /* return error */
3883 }
3884 if (top >= bottom) /* check top and bottom */
3885 {
3886 handle->debug_print("st7789: top >= bottom.\n"); /* top >= bottom */
3887
3888 return 9; /* return error */
3889 }
3890
3891 if (a_st7789_write_byte(handle, ST7789_CMD_CASET, ST7789_CMD) != 0) /* write set column address command */
3892 {
3893 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3894
3895 return 1; /* return error */
3896 }
3897 buf[0] = (left >> 8) & 0xFF; /* start address msb */
3898 buf[1] = (left >> 0) & 0xFF; /* start address lsb */
3899 buf[2] = ((right) >> 8) & 0xFF; /* end address msb */
3900 buf[3] = ((right) >> 0) & 0xFF; /* end address lsb */
3901 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
3902 {
3903 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3904
3905 return 1; /* return error */
3906 }
3907
3908 if (a_st7789_write_byte(handle, ST7789_CMD_RASET, ST7789_CMD) != 0) /* write set row address command */
3909 {
3910 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3911
3912 return 1; /* return error */
3913 }
3914 buf[0] = (top >> 8) & 0xFF; /* start address msb */
3915 buf[1] = (top >> 0) & 0xFF; /* start address lsb */
3916 buf[2] = ((bottom) >> 8) & 0xFF; /* end address msb */
3917 buf[3] = ((bottom) >> 0) & 0xFF; /* end address lsb */
3918 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
3919 {
3920 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3921
3922 return 1; /* return error */
3923 }
3924
3925 if (a_st7789_write_byte(handle, ST7789_CMD_RAMWR, ST7789_CMD) != 0) /* write memory write command */
3926 {
3927 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
3928
3929 return 1; /* return error */
3930 }
3931
3932 if ((handle->format & 0x03) == 0x03) /* rgb444 */
3933 {
3934 uint16_t r;
3935 uint16_t c;
3936 uint16_t color;
3937
3938 c = right - left + 1; /* column */
3939 r = bottom - top + 1; /* row */
3940 point = 0; /* image point init 0 */
3941 m = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 3 / 2) /
3942 ST7789_BUFFER_SIZE; /* total times */
3943 n = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 3 / 2) %
3944 ST7789_BUFFER_SIZE; /* the last */
3945 for (i = 0; i < m; i++)
3946 {
3947 for (j = 0; j < ST7789_BUFFER_SIZE; j += 3) /* fill the buffer */
3948 {
3949 color = image[(point % c) * r + (point / c)]; /* set color */
3950 handle->buf[i] = (((color >> 8) & 0xF) << 4) |
3951 (((color >> 4) & 0xF) << 0); /* set the color */
3952 handle->buf[i + 1] = (((color >> 0) & 0xF) << 4) |
3953 (((color >> 8) & 0xF) << 0); /* set the color */
3954 handle->buf[i + 2] = (((color >> 4) & 0xF) << 4) |
3955 (((color >> 0) & 0xF) << 0); /* set the color */
3956 point++; /* point++ */
3957 }
3958 if (a_st7789_write_bytes(handle, handle->buf,
3959 ST7789_BUFFER_SIZE, ST7789_DATA) != 0) /* write data */
3960 {
3961 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3962
3963 return 1; /* return error */
3964 }
3965 }
3966 if (n != 0) /* not end */
3967 {
3968 for (j = 0; j < n; j += 3) /* fill the buffer */
3969 {
3970 color = image[(point % c) * r + (point / c)]; /* set color */
3971 handle->buf[i] = (((color >> 8) & 0xF) << 4) |
3972 (((color >> 4) & 0xF) << 0); /* set the color */
3973 handle->buf[i + 1] = (((color >> 0) & 0xF) << 4) |
3974 (((color >> 8) & 0xF) << 0); /* set the color */
3975 handle->buf[i + 2] = (((color >> 4) & 0xF) << 4) |
3976 (((color >> 0) & 0xF) << 0); /* set the color */
3977 point++; /* point++ */
3978 }
3979 if (a_st7789_write_bytes(handle, handle->buf, n, ST7789_DATA) != 0) /* write data */
3980 {
3981 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
3982
3983 return 1; /* return error */
3984 }
3985 }
3986 }
3987 else
3988 {
3989 handle->debug_print("st7789: format is invalid.\n"); /* format is invalid */
3990
3991 return 4; /* return error */
3992 }
3993
3994 return 0; /* success return 0 */
3995}
3996
4018uint8_t st7789_draw_picture_16bits(st7789_handle_t *handle, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint16_t *image)
4019{
4020 uint8_t buf[4];
4021 uint32_t i;
4022 uint32_t j;
4023 uint32_t m;
4024 uint32_t n;
4025 uint32_t point;
4026
4027 if (handle == NULL) /* check handle */
4028 {
4029 return 2; /* return error */
4030 }
4031 if (handle->inited != 1) /* check handle initialization */
4032 {
4033 return 3; /* return error */
4034 }
4035 if (left > (handle->column - 1)) /* check left */
4036 {
4037 handle->debug_print("st7789: left is over column.\n"); /* left is over column */
4038
4039 return 4; /* return error */
4040 }
4041 if (right > (handle->column - 1)) /* check right */
4042 {
4043 handle->debug_print("st7789: right is over column.\n"); /* right is over column */
4044
4045 return 5; /* return error */
4046 }
4047 if (left >= right) /* check left and right */
4048 {
4049 handle->debug_print("st7789: left >= right.\n"); /* left >= right */
4050
4051 return 6; /* return error */
4052 }
4053 if (top > (handle->row - 1)) /* check top */
4054 {
4055 handle->debug_print("st7789: top is over row.\n"); /* top is over row */
4056
4057 return 7; /* return error */
4058 }
4059 if (bottom > (handle->row - 1)) /* check bottom */
4060 {
4061 handle->debug_print("st7789: bottom is over row.\n"); /* bottom is over row */
4062
4063 return 8; /* return error */
4064 }
4065 if (top >= bottom) /* check top and bottom */
4066 {
4067 handle->debug_print("st7789: top >= bottom.\n"); /* top >= bottom */
4068
4069 return 9; /* return error */
4070 }
4071
4072 if (a_st7789_write_byte(handle, ST7789_CMD_CASET, ST7789_CMD) != 0) /* write set column address command */
4073 {
4074 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
4075
4076 return 1; /* return error */
4077 }
4078 buf[0] = (left >> 8) & 0xFF; /* start address msb */
4079 buf[1] = (left >> 0) & 0xFF; /* start address lsb */
4080 buf[2] = ((right) >> 8) & 0xFF; /* end address msb */
4081 buf[3] = ((right) >> 0) & 0xFF; /* end address lsb */
4082 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
4083 {
4084 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4085
4086 return 1; /* return error */
4087 }
4088
4089 if (a_st7789_write_byte(handle, ST7789_CMD_RASET, ST7789_CMD) != 0) /* write set row address command */
4090 {
4091 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
4092
4093 return 1; /* return error */
4094 }
4095 buf[0] = (top >> 8) & 0xFF; /* start address msb */
4096 buf[1] = (top >> 0) & 0xFF; /* start address lsb */
4097 buf[2] = ((bottom) >> 8) & 0xFF; /* end address msb */
4098 buf[3] = ((bottom) >> 0) & 0xFF; /* end address lsb */
4099 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
4100 {
4101 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4102
4103 return 1; /* return error */
4104 }
4105
4106 if (a_st7789_write_byte(handle, ST7789_CMD_RAMWR, ST7789_CMD) != 0) /* write memory write command */
4107 {
4108 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
4109
4110 return 1; /* return error */
4111 }
4112
4113 if ((handle->format & 0x05) == 0x05) /* rgb565 */
4114 {
4115 uint16_t r;
4116 uint16_t c;
4117 uint16_t color;
4118
4119 c = right - left + 1; /* column */
4120 r = bottom - top + 1; /* row */
4121 point = 0; /* image point init 0 */
4122 m = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 2) /
4123 ST7789_BUFFER_SIZE; /* total times */
4124 n = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 2) %
4125 ST7789_BUFFER_SIZE; /* the last */
4126 for (i = 0; i < m; i++)
4127 {
4128 for (j = 0; j < ST7789_BUFFER_SIZE; j += 2) /* fill the buffer */
4129 {
4130 color = image[(point % c) * r + (point / c)]; /* set color */
4131 handle->buf[j] = (color >> 8) & 0xFF; /* set the color */
4132 handle->buf[j + 1] = (color >> 0) & 0xFF; /* set the color */
4133 point++; /* point++ */
4134 }
4135 if (a_st7789_write_bytes(handle, handle->buf,
4136 ST7789_BUFFER_SIZE, ST7789_DATA) != 0) /* write data */
4137 {
4138 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4139
4140 return 1; /* return error */
4141 }
4142 }
4143 if (n != 0) /* not end */
4144 {
4145 for (j = 0; j < n; j += 2) /* fill the buffer */
4146 {
4147 color = image[(point % c) * r + (point / c)]; /* set color */
4148 handle->buf[j] = (color >> 8) & 0xFF; /* set the color */
4149 handle->buf[j + 1] = (color >> 0) & 0xFF; /* set the color */
4150 point++; /* point++ */
4151 }
4152 if (a_st7789_write_bytes(handle, handle->buf, n, ST7789_DATA) != 0) /* write data */
4153 {
4154 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4155
4156 return 1; /* return error */
4157 }
4158 }
4159 }
4160 else
4161 {
4162 handle->debug_print("st7789: format is invalid.\n"); /* format is invalid */
4163
4164 return 4; /* return error */
4165 }
4166
4167 return 0; /* success return 0 */
4168}
4169
4191uint8_t st7789_draw_picture_18bits(st7789_handle_t *handle, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint32_t *image)
4192{
4193 uint8_t buf[4];
4194 uint32_t i;
4195 uint32_t j;
4196 uint32_t m;
4197 uint32_t n;
4198 uint32_t point;
4199
4200 if (handle == NULL) /* check handle */
4201 {
4202 return 2; /* return error */
4203 }
4204 if (handle->inited != 1) /* check handle initialization */
4205 {
4206 return 3; /* return error */
4207 }
4208 if (left > (handle->column - 1)) /* check left */
4209 {
4210 handle->debug_print("st7789: left is over column.\n"); /* left is over column */
4211
4212 return 4; /* return error */
4213 }
4214 if (right > (handle->column - 1)) /* check right */
4215 {
4216 handle->debug_print("st7789: right is over column.\n"); /* right is over column */
4217
4218 return 5; /* return error */
4219 }
4220 if (left >= right) /* check left and right */
4221 {
4222 handle->debug_print("st7789: left >= right.\n"); /* left >= right */
4223
4224 return 6; /* return error */
4225 }
4226 if (top > (handle->row - 1)) /* check top */
4227 {
4228 handle->debug_print("st7789: top is over row.\n"); /* top is over row */
4229
4230 return 7; /* return error */
4231 }
4232 if (bottom > (handle->row - 1)) /* check bottom */
4233 {
4234 handle->debug_print("st7789: bottom is over row.\n"); /* bottom is over row */
4235
4236 return 8; /* return error */
4237 }
4238 if (top >= bottom) /* check top and bottom */
4239 {
4240 handle->debug_print("st7789: top >= bottom.\n"); /* top >= bottom */
4241
4242 return 9; /* return error */
4243 }
4244
4245 if (a_st7789_write_byte(handle, ST7789_CMD_CASET, ST7789_CMD) != 0) /* write set column address command */
4246 {
4247 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
4248
4249 return 1; /* return error */
4250 }
4251 buf[0] = (left >> 8) & 0xFF; /* start address msb */
4252 buf[1] = (left >> 0) & 0xFF; /* start address lsb */
4253 buf[2] = ((right) >> 8) & 0xFF; /* end address msb */
4254 buf[3] = ((right) >> 0) & 0xFF; /* end address lsb */
4255 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
4256 {
4257 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4258
4259 return 1; /* return error */
4260 }
4261
4262 if (a_st7789_write_byte(handle, ST7789_CMD_RASET, ST7789_CMD) != 0) /* write set row address command */
4263 {
4264 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
4265
4266 return 1; /* return error */
4267 }
4268 buf[0] = (top >> 8) & 0xFF; /* start address msb */
4269 buf[1] = (top >> 0) & 0xFF; /* start address lsb */
4270 buf[2] = ((bottom) >> 8) & 0xFF; /* end address msb */
4271 buf[3] = ((bottom) >> 0) & 0xFF; /* end address lsb */
4272 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
4273 {
4274 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4275
4276 return 1; /* return error */
4277 }
4278
4279 if (a_st7789_write_byte(handle, ST7789_CMD_RAMWR, ST7789_CMD) != 0) /* write memory write command */
4280 {
4281 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
4282
4283 return 1; /* return error */
4284 }
4285
4286 if ((handle->format & 0x06) == 0x06) /* rgb666 */
4287 {
4288 uint16_t r;
4289 uint16_t c;
4290 uint32_t color;
4291
4292 c = right - left + 1; /* column */
4293 r = bottom - top + 1; /* row */
4294 point = 0; /* image point init 0 */
4295 m = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 3) /
4296 ST7789_BUFFER_SIZE; /* total times */
4297 n = ((uint32_t)(right - left + 1) * (bottom - top + 1) * 3) %
4298 ST7789_BUFFER_SIZE; /* the last */
4299 for (i = 0; i < m; i++)
4300 {
4301 for (j = 0; j < ST7789_BUFFER_SIZE; j += 3) /* fill the buffer */
4302 {
4303 color = image[(point % c) * r + (point / c)]; /* set color */
4304 handle->buf[j] = ((color >> 12) & 0x3F) << 2; /* set the color */
4305 handle->buf[j + 1] = ((color >> 6) & 0x3F) << 2; /* set the color */
4306 handle->buf[j + 2] = ((color >> 0) & 0x3F) << 2; /* set the color */
4307 point++; /* point++ */
4308 }
4309 if (a_st7789_write_bytes(handle, handle->buf,
4310 ST7789_BUFFER_SIZE, ST7789_DATA) != 0) /* write data */
4311 {
4312 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4313
4314 return 1; /* return error */
4315 }
4316 }
4317 if (n != 0) /* not end */
4318 {
4319 for (j = 0; j < n; j += 3) /* fill the buffer */
4320 {
4321 color = image[(point % c) * r + (point / c)]; /* set color */
4322 handle->buf[j] = ((color >> 12) & 0x3F) << 2; /* set the color */
4323 handle->buf[j + 1] = ((color >> 6) & 0x3F) << 2; /* set the color */
4324 handle->buf[j + 2] = ((color >> 0) & 0x3F) << 2; /* set the color */
4325 point++; /* point++ */
4326 }
4327 if (a_st7789_write_bytes(handle, handle->buf, n, ST7789_DATA) != 0) /* write data */
4328 {
4329 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4330
4331 return 1; /* return error */
4332 }
4333 }
4334 }
4335 else
4336 {
4337 handle->debug_print("st7789: format is invalid.\n"); /* format is invalid */
4338
4339 return 4; /* return error */
4340 }
4341
4342 return 0; /* success return 0 */
4343}
4344
4356static uint8_t a_st7789_draw_point(st7789_handle_t *handle, uint16_t x, uint16_t y, uint32_t color)
4357{
4358 uint8_t buf[4];
4359
4360 if (a_st7789_write_byte(handle, ST7789_CMD_CASET, ST7789_CMD) != 0) /* write set column address command */
4361 {
4362 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
4363
4364 return 1; /* return error */
4365 }
4366 buf[0] = (x >> 8) & 0xFF; /* start address msb */
4367 buf[1] = (x >> 0) & 0xFF; /* start address lsb */
4368 buf[2] = (x >> 8) & 0xFF; /* end address msb */
4369 buf[3] = (x >> 0) & 0xFF; /* end address lsb */
4370 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
4371 {
4372 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4373
4374 return 1; /* return error */
4375 }
4376
4377 if (a_st7789_write_byte(handle, ST7789_CMD_RASET, ST7789_CMD) != 0) /* write set row address command */
4378 {
4379 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
4380
4381 return 1; /* return error */
4382 }
4383 buf[0] = (y >> 8) & 0xFF; /* start address msb */
4384 buf[1] = (y >> 0) & 0xFF; /* start address lsb */
4385 buf[2] = (y >> 8) & 0xFF; /* end address msb */
4386 buf[3] = (y >> 0) & 0xFF; /* end address lsb */
4387 if (a_st7789_write_bytes(handle, buf, 4, ST7789_DATA) != 0) /* write data */
4388 {
4389 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4390
4391 return 1; /* return error */
4392 }
4393
4394 if (a_st7789_write_byte(handle, ST7789_CMD_RAMWR, ST7789_CMD) != 0) /* write memory write command */
4395 {
4396 handle->debug_print("st7789: write command failed.\n"); /* write command failed */
4397
4398 return 1; /* return error */
4399 }
4400
4401 if ((handle->format & 0x03) == 0x03) /* rgb444 */
4402 {
4403 handle->buf[0] = (((color >> 8) & 0xF) << 4) |
4404 (((color >> 4) & 0xF) << 0); /* set the color */
4405 handle->buf[1] = (((color >> 0) & 0xF) << 4); /* set the color */
4406 if (a_st7789_write_bytes(handle, handle->buf,
4407 2, ST7789_DATA) != 0) /* write data */
4408 {
4409 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4410
4411 return 1; /* return error */
4412 }
4413 }
4414 else if ((handle->format & 0x05) == 0x05) /* rgb565 */
4415 {
4416 handle->buf[0] = (color >> 8) & 0xFF; /* set the color */
4417 handle->buf[1] = (color >> 0) & 0xFF; /* set the color */
4418 if (a_st7789_write_bytes(handle, handle->buf,
4419 2, ST7789_DATA) != 0) /* write data */
4420 {
4421 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4422
4423 return 1; /* return error */
4424 }
4425 }
4426 else if ((handle->format & 0x06) == 0x06) /* rgb666 */
4427 {
4428 handle->buf[0] = ((color >> 12) & 0x3F) << 2; /* set the color */
4429 handle->buf[1] = ((color >> 6) & 0x3F) << 2; /* set the color */
4430 handle->buf[2] = ((color >> 0) & 0x3F) << 2; /* set the color */
4431 if (a_st7789_write_bytes(handle, handle->buf,
4432 3, ST7789_DATA) != 0) /* write data */
4433 {
4434 handle->debug_print("st7789: write data failed.\n"); /* write data failed */
4435
4436 return 1; /* return error */
4437 }
4438 }
4439 else
4440 {
4441 handle->debug_print("st7789: format is invalid.\n"); /* format is invalid */
4442
4443 return 4; /* return error */
4444 }
4445
4446 return 0; /* success return 0 */
4447}
4448
4462static uint8_t a_st7789_show_char(st7789_handle_t *handle, uint16_t x, uint16_t y, uint8_t chr, uint8_t size, uint32_t color)
4463{
4464 uint8_t temp, t, t1;
4465 uint16_t y0 = y;
4466 uint8_t csize = (size / 8 + ((size % 8) ? 1 : 0)) * (size / 2); /* get size */
4467
4468 chr = chr - ' '; /* get index */
4469 for (t = 0; t < csize; t++) /* write size */
4470 {
4471 if (size == 12) /* if size 12 */
4472 {
4473 temp = gsc_st7789_ascii_1206[chr][t]; /* get ascii 1206 */
4474 }
4475 else if (size == 16) /* if size 16 */
4476 {
4477 temp = gsc_st7789_ascii_1608[chr][t]; /* get ascii 1608 */
4478 }
4479 else if(size == 24) /* if size 24 */
4480 {
4481 temp = gsc_st7789_ascii_2412[chr][t]; /* get ascii 2412 */
4482 }
4483 else
4484 {
4485 return 1; /* return error */
4486 }
4487 for (t1 = 0; t1 < 8; t1++) /* write one line */
4488 {
4489 if ((temp & 0x80) != 0) /* if 1 */
4490 {
4491 if (a_st7789_draw_point(handle, x, y, color) != 0) /* draw point */
4492 {
4493 return 1; /* return error */
4494 }
4495 }
4496 temp <<= 1; /* left shift 1 */
4497 y++;
4498 if ((y - y0) == size) /* reset size */
4499 {
4500 y = y0; /* set y */
4501 x++; /* x++ */
4502
4503 break; /* break */
4504 }
4505 }
4506 }
4507
4508 return 0; /* success return 0 */
4509}
4510
4528uint8_t st7789_write_string(st7789_handle_t *handle, uint16_t x, uint16_t y, char *str, uint16_t len, uint32_t color, st7789_font_t font)
4529{
4530 if (handle == NULL) /* check handle */
4531 {
4532 return 2; /* return error */
4533 }
4534 if (handle->inited != 1) /* check handle initialization */
4535 {
4536 return 3; /* return error */
4537 }
4538 if((x >= handle->column) || (y >= handle->row)) /* check x, y */
4539 {
4540 handle->debug_print("ssd1351: x or y is invalid.\n"); /* x or y is invalid */
4541
4542 return 4; /* return error */
4543 }
4544
4545 while ((len != 0) && (*str <= '~') && (*str >= ' ')) /* write all string */
4546 {
4547 if (x >= (handle->column - (font / 2))) /* check x point */
4548 {
4549 x = 0; /* set x */
4550 y += (uint8_t)font; /* set next row */
4551 }
4552 if (y >= (handle->row - font)) /* check y pont */
4553 {
4554 y = x = 0; /* reset to 0 */
4555 }
4556 if (a_st7789_show_char(handle, x, y, *str, font, color) != 0) /* show a char */
4557 {
4558 return 1; /* return error */
4559 }
4560 x += (uint8_t)(font / 2); /* x + font/2 */
4561 str++; /* str address++ */
4562 len--; /* str length-- */
4563 }
4564
4565 return 0; /* success return 0 */
4566}
4567
4583uint8_t st7789_draw_point(st7789_handle_t *handle, uint16_t x, uint16_t y, uint32_t color)
4584{
4585 if (handle == NULL) /* check handle */
4586 {
4587 return 2; /* return error */
4588 }
4589 if (handle->inited != 1) /* check handle initialization */
4590 {
4591 return 3; /* return error */
4592 }
4593 if (x >= handle->column) /* check x */
4594 {
4595 handle->debug_print("ssd1351: x is over column.\n"); /* x is over column */
4596
4597 return 4; /* return error */
4598 }
4599 if (y >= handle->row) /* check y */
4600 {
4601 handle->debug_print("ssd1351: y is over row.\n"); /* y is over row */
4602
4603 return 5; /* return error */
4604 }
4605
4606 return a_st7789_draw_point(handle, x, y, color); /* draw point */
4607}
4608
4620uint8_t st7789_write_cmd(st7789_handle_t *handle, uint8_t cmd)
4621{
4622 if (handle == NULL) /* check handle */
4623 {
4624 return 2; /* return error */
4625 }
4626 if (handle->inited != 1) /* check handle initialization */
4627 {
4628 return 3; /* return error */
4629 }
4630
4631 return a_st7789_write_byte(handle, cmd, ST7789_CMD); /* write command */
4632}
4633
4645uint8_t st7789_write_data(st7789_handle_t *handle, uint8_t data)
4646{
4647 if (handle == NULL) /* check handle */
4648 {
4649 return 2; /* return error */
4650 }
4651 if (handle->inited != 1) /* check handle initialization */
4652 {
4653 return 3; /* return error */
4654 }
4655
4656 return a_st7789_write_byte(handle, data, ST7789_DATA); /* write data */
4657}
4658
4668{
4669 if (info == NULL) /* check handle */
4670 {
4671 return 2; /* return error */
4672 }
4673
4674 memset(info, 0, sizeof(st7789_info_t)); /* initialize st7789 info structure */
4675 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
4676 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
4677 strncpy(info->interface, "SPI", 8); /* copy interface name */
4678 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
4679 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
4680 info->max_current_ma = MAX_CURRENT; /* set maximum current */
4681 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
4682 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
4683 info->driver_version = DRIVER_VERSION; /* set driver version */
4684
4685 return 0; /* success return 0 */
4686}
#define ST7789_CMD_RAMWR
#define ST7789_CMD_GAMSET
#define ST7789_CMD_VCOMS
#define ST7789_CMD_RGBCTRL
#define ST7789_CMD_VRHS
#define ST7789_CMD_PORCTRL
#define ST7789_CMD_DISPON
#define ST7789_CMD_IDMOFF
#define ST7789_CMD_PTLON
#define MAX_CURRENT
#define ST7789_CMD_SLPIN
#define ST7789_CMD_PTLAR
#define ST7789_CMD_PVGAMCTRL
#define ST7789_CMD_IDMON
#define ST7789_CMD_INVON
#define ST7789_CMD_WRCABCMB
#define ST7789_CMD_COLMOD
#define ST7789_CMD_EQCTRL
#define ST7789_CMD_CASET
#define ST7789_CMD_PROMCTRL
#define SUPPLY_VOLTAGE_MAX
#define ST7789_CMD_PROMEN
#define ST7789_CMD_GCTRL
#define ST7789_CMD_WRDISBV
#define ST7789_CMD_DGMEN
#define ST7789_CMD_CMD2EN
#define ST7789_CMD_GTADJ
#define ST7789_CMD_WRCACE
#define ST7789_CMD_WRCTRLD
#define ST7789_CMD_GATECTRL
#define ST7789_CMD_PWMFRSEL
#define ST7789_CMD_SWRESET
#define ST7789_CMD_RASET
#define ST7789_CMD_PWCTRL2
#define TEMPERATURE_MAX
#define ST7789_CMD_RAMWRC
#define ST7789_CMD_PARCTRL
#define ST7789_CMD_VSCRSADD
#define ST7789_CMD_VSCRDEF
#define ST7789_CMD_VDVVRHEN
#define ST7789_CMD_DGMLUTR
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define ST7789_CMD_NORON
#define SUPPLY_VOLTAGE_MIN
#define ST7789_CMD_MADCTL
#define ST7789_CMD_NVGAMCTRL
#define ST7789_CMD_TEOFF
#define ST7789_CMD_PROMACT
#define ST7789_CMD_NOP
chip command definition
#define ST7789_CMD_RAMCTRL
#define ST7789_CMD_TEON
#define ST7789_CMD_FRCTR2
#define ST7789_CMD_LCMCTRL
#define ST7789_CMD_SLPOUT
#define ST7789_CMD_PWCTRL1
#define ST7789_CMD_VAPVANEN
#define ST7789_CMD_VCMOFSET
#define ST7789_CMD
command data type definition
#define ST7789_CMD_INVOFF
#define ST7789_CMD_DGMLUTB
#define ST7789_CMD_SPI2EN
#define ST7789_CMD_NVMSET
#define CHIP_NAME
chip information definition
#define ST7789_CMD_CABCCTRL
#define ST7789_CMD_DISPOFF
#define DRIVER_VERSION
#define ST7789_DATA
#define ST7789_CMD_VDVSET
#define ST7789_CMD_FRCTRL1
#define ST7789_CMD_IDSET
#define ST7789_CMD_TESCAN
driver st7789 header file
driver st7789 font header file
st7789_frame_rate_t
st7789 frame rate enumeration definition
st7789_vds_t
st7789 vds enumeration definition
uint8_t st7789_set_id_code_setting(st7789_handle_t *handle, uint8_t id[3])
set id code setting
uint8_t st7789_vrhs_convert_to_register(st7789_handle_t *handle, float v, uint8_t *reg)
convert the vrhs to the register raw data
st7789_rgb_bus_width_t
st7789 rgb bus width enumeration definition
uint8_t st7789_gate_line_convert_to_data(st7789_handle_t *handle, uint8_t reg, uint16_t *l)
convert the register raw data to the gate line
st7789_avdd_t
st7789 avdd enumeration definition
uint8_t st7789_enable_vap_van_signal_output(st7789_handle_t *handle)
enable vap van signal output
uint8_t st7789_set_program_action(st7789_handle_t *handle)
set program action
uint8_t st7789_set_gate_on_timing_adjustment(st7789_handle_t *handle, uint8_t gate_on_timing_adjustment, uint8_t gate_off_timing_adjustment_rgb, uint8_t gate_off_timing_adjustment)
set gate on timing adjustment
st7789_stp14ck_div_t
st7789 stp14ck div enumeration definition
st7789_vdv_vrh_from_t
st7789 vdv vrh from enumeration definition
uint8_t st7789_set_nvm_setting(st7789_handle_t *handle, uint8_t addr, uint8_t data)
set nvm setting
st7789_frame_rate_divided_control_t
st7789 frame rate divided control enumeration definition
st7789_pixel_type_t
st7789 pixel type enumeration definition
uint8_t st7789_vcoms_offset_convert_to_data(st7789_handle_t *handle, uint8_t reg, float *v)
convert the register raw data to the vcoms offset
uint8_t st7789_vdv_convert_to_register(st7789_handle_t *handle, float v, uint8_t *reg)
convert the vdv to the register raw data
uint8_t st7789_set_vcoms_offset(st7789_handle_t *handle, uint8_t offset)
set vcoms offset
uint8_t st7789_set_rgb_interface_control(st7789_handle_t *handle, st7789_direct_rgb_mode_t rgb_mode, st7789_rgb_if_enable_mode_t rgb_if_mode, st7789_pin_level_t vspl, st7789_pin_level_t hspl, st7789_pin_level_t dpl, st7789_pin_level_t epl, uint8_t vbp, uint8_t hbp)
set rgb interface control
uint8_t st7789_set_program_mode_enable(st7789_handle_t *handle, st7789_bool_t enable)
enable or disable program mode
uint8_t st7789_vcom_convert_to_data(st7789_handle_t *handle, uint8_t reg, float *v)
convert the register raw data to the vcom
uint8_t st7789_set_command_2_enable(st7789_handle_t *handle, st7789_bool_t enable)
enable or disable command 2
st7789_inversion_selection_t
st7789 inversion selection enumeration definition
uint8_t st7789_set_equalize_time_control(st7789_handle_t *handle, uint8_t source_equalize_time, uint8_t source_pre_drive_time, uint8_t gate_equalize_time)
set equalize time control
st7789_pin_level_t
st7789 pin level enumeration definition
st7789_vgls_t
st7789 vgls enumeration definition
st7789_inversion_partial_mode_t
st7789 inversion partial mode enumeration definition
st7789_non_display_frame_frequency_t
st7789 non display frame frequency enumeration definition
uint8_t st7789_set_positive_voltage_gamma_control(st7789_handle_t *handle, uint8_t param[14])
set positive voltage gamma control
st7789_avcl_t
st7789 avcl enumeration definition
uint8_t st7789_set_program_mode_control(st7789_handle_t *handle)
set program mode control
st7789_pwm_frequency_t
st7789 pwm frequency enumeration definition
uint8_t st7789_set_vdv_vrh_from(st7789_handle_t *handle, st7789_vdv_vrh_from_t from)
set vdv vrh from
uint8_t st7789_set_vcoms(st7789_handle_t *handle, uint8_t vcoms)
set vcoms
uint8_t st7789_set_digital_gamma(st7789_handle_t *handle, st7789_bool_t enable)
enable or disable digital gamma
st7789_sbclk_div_t
st7789 sbclk div enumeration definition
uint8_t st7789_set_frame_rate(st7789_handle_t *handle, st7789_inversion_selection_t selection, st7789_frame_rate_t rate)
set frame rate
uint8_t st7789_set_negative_voltage_gamma_control(st7789_handle_t *handle, uint8_t param[14])
set negative voltage gamma control
uint8_t st7789_set_lcm_control(st7789_handle_t *handle, st7789_bool_t xmy, st7789_bool_t xbgr, st7789_bool_t xinv, st7789_bool_t xmx, st7789_bool_t xmh, st7789_bool_t xmv, st7789_bool_t xgs)
set lcm control
uint8_t st7789_set_power_control_1(st7789_handle_t *handle, st7789_avdd_t avdd, st7789_avcl_t avcl, st7789_vds_t vds)
set power control 1
uint8_t st7789_vrhs_convert_to_data(st7789_handle_t *handle, uint8_t reg, float *v)
convert the register raw data to the vrhs
st7789_frame_type_t
st7789 frame type enumeration definition
uint8_t st7789_set_frame_rate_control(st7789_handle_t *handle, st7789_bool_t separate_fr_control, st7789_frame_rate_divided_control_t div_control, st7789_inversion_idle_mode_t idle_mode, uint8_t idle_frame_rate, st7789_inversion_partial_mode_t partial_mode, uint8_t partial_frame_rate)
set frame rate control
st7789_vghs_t
st7789 vghs enumeration definition
st7789_non_display_source_output_level_t
st7789 non display source output level enumeration definition
st7789_data_mode_t
st7789 data mode enumeration definition
uint8_t st7789_set_ram_control(st7789_handle_t *handle, st7789_ram_access_t ram_mode, st7789_display_mode_t display_mode, st7789_frame_type_t frame_type, st7789_data_mode_t data_mode, st7789_rgb_bus_width_t bus_width, st7789_pixel_type_t pixel_type)
set ram control
uint8_t st7789_vcoms_offset_convert_to_register(st7789_handle_t *handle, float v, uint8_t *reg)
convert the vcoms offset to the register raw data
uint8_t st7789_set_gate(st7789_handle_t *handle, uint8_t gate_line_number, uint8_t first_scan_line_number, st7789_gate_scan_mode_t mode, st7789_gate_scan_direction_t direction)
set gate
st7789_inversion_idle_mode_t
st7789 inversion idle mode enumeration definition
uint8_t st7789_set_gate_control(st7789_handle_t *handle, st7789_vghs_t vghs, st7789_vgls_t vgls)
set gate control
uint8_t st7789_set_cabc_control(st7789_handle_t *handle, st7789_bool_t led_on, st7789_bool_t led_pwm_init, st7789_bool_t led_pwm_fix, st7789_bool_t led_pwm_polarity)
set cabc control
st7789_direct_rgb_mode_t
st7789 direct rgb mode enumeration definition
uint8_t st7789_set_partial_mode_control(st7789_handle_t *handle, st7789_non_display_source_output_level_t level, st7789_non_display_area_scan_mode_t mode, st7789_non_display_frame_frequency_t frequency)
set partial mode control
uint8_t st7789_vdv_convert_to_data(st7789_handle_t *handle, uint8_t reg, float *v)
convert the register raw data to the vdv
st7789_non_display_area_scan_mode_t
st7789 non display area scan mode enumeration definition
st7789_rgb_if_enable_mode_t
st7789 rgb if enable mode enumeration definition
st7789_ram_access_t
st7789 ram access enumeration definition
uint8_t st7789_set_pwm_frequency(st7789_handle_t *handle, st7789_pwm_frequency_t frequency)
set pwm frequency
uint8_t st7789_set_spi2_enable(st7789_handle_t *handle, st7789_bool_t date_lane, st7789_bool_t command_table_2)
set spi2 enable
uint8_t st7789_set_porch(st7789_handle_t *handle, uint8_t back_porch_normal, uint8_t front_porch_normal, st7789_bool_t separate_porch_enable, uint8_t back_porch_idle, uint8_t front_porch_idle, uint8_t back_porch_partial, uint8_t front_porch_partial)
set porch
uint8_t st7789_set_power_control_2(st7789_handle_t *handle, st7789_sbclk_div_t sbclk, st7789_stp14ck_div_t stp14ck)
set power control 2
uint8_t st7789_vcom_convert_to_register(st7789_handle_t *handle, float v, uint8_t *reg)
convert the vcom to the register raw data
uint8_t st7789_set_vdv(st7789_handle_t *handle, uint8_t vdv)
set vdv
st7789_gate_scan_direction_t
st7789 gate scan direction enumeration definition
st7789_gate_scan_mode_t
st7789 gate scan mode enumeration definition
uint8_t st7789_set_vrhs(st7789_handle_t *handle, uint8_t vrhs)
set vrhs
uint8_t st7789_set_digital_gamma_look_up_table_blue(st7789_handle_t *handle, uint8_t param[64])
set blue digital gamma look up table
uint8_t st7789_set_digital_gamma_look_up_table_red(st7789_handle_t *handle, uint8_t param[64])
set red digital gamma look up table
uint8_t st7789_gate_line_convert_to_register(st7789_handle_t *handle, uint16_t l, uint8_t *reg)
convert the gate line to the register raw data
st7789_display_mode_t
st7789 display mode enumeration definition
uint8_t st7789_display_inversion_off(st7789_handle_t *handle)
display inversion off
st7789_font_t
st7789 font size enumeration definition
uint8_t st7789_draw_picture_18bits(st7789_handle_t *handle, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint32_t *image)
draw a picture
uint8_t st7789_set_vertical_scrolling(st7789_handle_t *handle, uint16_t top_fixed_area, uint16_t scrolling_area, uint16_t bottom_fixed_area)
set vertical scrolling
uint8_t st7789_display_on(st7789_handle_t *handle)
display on
uint8_t st7789_tearing_effect_line_on(st7789_handle_t *handle, st7789_tearing_effect_t effect)
tearing effect line on
uint8_t st7789_set_vertical_scroll_start_address(st7789_handle_t *handle, uint16_t start_address)
set the vertical scroll start address
uint8_t st7789_init(st7789_handle_t *handle)
initialize the chip
uint8_t st7789_set_tear_scanline(st7789_handle_t *handle, uint16_t l)
set tear scanline
uint8_t st7789_draw_picture_12bits(st7789_handle_t *handle, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint16_t *image)
draw a picture
st7789_color_enhancement_mode_t
st7789 color enhancement mode enumeration definition
uint8_t st7789_set_column_address(st7789_handle_t *handle, uint16_t start_address, uint16_t end_address)
set the column address
struct st7789_handle_s st7789_handle_t
st7789 handle structure definition
#define ST7789_BUFFER_SIZE
st7789 buffer size definition
uint8_t st7789_set_row(st7789_handle_t *handle, uint16_t row)
set row
uint8_t st7789_normal_display_mode_on(st7789_handle_t *handle)
normal display mode on
uint8_t st7789_write_string(st7789_handle_t *handle, uint16_t x, uint16_t y, char *str, uint16_t len, uint32_t color, st7789_font_t font)
write a string in the display
uint8_t st7789_software_reset(st7789_handle_t *handle)
software reset
uint8_t st7789_set_partial_areas(st7789_handle_t *handle, uint16_t start_row, uint16_t end_row)
set partial areas
st7789_rgb_interface_color_format_t
st7789 rgb interface color format enumeration definition
uint8_t st7789_set_display_control(st7789_handle_t *handle, st7789_bool_t brightness_control_block, st7789_bool_t display_dimming, st7789_bool_t backlight_control)
set display control
uint8_t st7789_fill_rect(st7789_handle_t *handle, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint32_t color)
fill the rect
uint8_t st7789_set_cabc_minimum_brightness(st7789_handle_t *handle, uint8_t brightness)
set cabc minimum brightness
uint8_t st7789_tearing_effect_line_off(st7789_handle_t *handle)
tearing effect line off
uint8_t st7789_set_interface_pixel_format(st7789_handle_t *handle, st7789_rgb_interface_color_format_t rgb, st7789_control_interface_color_format_t control)
set interface pixel format
uint8_t st7789_idle_mode_off(st7789_handle_t *handle)
idle mode off
uint8_t st7789_idle_mode_on(st7789_handle_t *handle)
idle mode on
uint8_t st7789_deinit(st7789_handle_t *handle)
close the chip
uint8_t st7789_nop(st7789_handle_t *handle)
nop
uint8_t st7789_partial_display_mode_on(st7789_handle_t *handle)
partial display mode on
uint8_t st7789_set_brightness_control_and_color_enhancement(st7789_handle_t *handle, st7789_bool_t color_enhancement, st7789_color_enhancement_mode_t mode, st7789_color_enhancement_level_t level)
set brightness control and color enhancement
uint8_t st7789_info(st7789_info_t *info)
get chip's information
st7789_color_enhancement_level_t
st7789 color enhancement level enumeration definition
uint8_t st7789_draw_picture_16bits(st7789_handle_t *handle, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint16_t *image)
draw a picture
uint8_t st7789_clear(st7789_handle_t *handle)
clear the display
uint8_t st7789_memory_continue_write(st7789_handle_t *handle, uint8_t *data, uint16_t len)
memory continue write
uint8_t st7789_display_off(st7789_handle_t *handle)
display off
st7789_bool_t
st7789 bool enumeration definition
uint8_t st7789_set_row_address(st7789_handle_t *handle, uint16_t start_address, uint16_t end_address)
set the row address
uint8_t st7789_memory_write(st7789_handle_t *handle, uint8_t *data, uint16_t len)
memory write
st7789_tearing_effect_t
st7789 tearing effect enumeration definition
struct st7789_info_s st7789_info_t
st7789 information structure definition
uint8_t st7789_sleep_in(st7789_handle_t *handle)
sleep in
uint8_t st7789_set_gamma(st7789_handle_t *handle, uint8_t gamma)
set gamma
st7789_control_interface_color_format_t
st7789 control interface color format enumeration definition
uint8_t st7789_set_display_brightness(st7789_handle_t *handle, uint8_t brightness)
set display brightness
uint8_t st7789_set_column(st7789_handle_t *handle, uint16_t column)
set column
uint8_t st7789_display_inversion_on(st7789_handle_t *handle)
display inversion on
uint8_t st7789_draw_point(st7789_handle_t *handle, uint16_t x, uint16_t y, uint32_t color)
draw a point in the display
uint8_t st7789_sleep_out(st7789_handle_t *handle)
sleep out
uint8_t st7789_set_memory_data_access_control(st7789_handle_t *handle, uint8_t order)
set memory data access control
uint8_t st7789_write_data(st7789_handle_t *handle, uint8_t data)
write the data
uint8_t st7789_write_cmd(st7789_handle_t *handle, uint8_t cmd)
write the command
uint8_t(* spi_init)(void)
uint8_t(* cmd_data_gpio_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* cmd_data_gpio_deinit)(void)
uint8_t(* reset_gpio_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint8_t(* reset_gpio_init)(void)
uint8_t(* cmd_data_gpio_write)(uint8_t value)
uint8_t(* spi_write_cmd)(uint8_t *buf, uint16_t len)
uint8_t buf[ST7789_BUFFER_SIZE+8]
uint8_t(* reset_gpio_write)(uint8_t value)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]