-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathst7789_display_driver.c
926 lines (745 loc) · 26.1 KB
/
st7789_display_driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
/*
* This file is part of AtomGL.
*
* Copyright 2020-2024 Davide Bettio <davide@uninstall.it>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "display_driver.h"
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/gpio.h>
#include <driver/spi_master.h>
#include <esp_heap_caps.h>
#include <esp_log.h>
#include <atom.h>
#include <bif.h>
#include <context.h>
#include <debug.h>
#include <defaultatoms.h>
#include <globalcontext.h>
#include <interop.h>
#include <mailbox.h>
#include <module.h>
#include <port.h>
#include <sys.h>
#include <term.h>
#include <utils.h>
#include <esp32_sys.h>
#include <trace.h>
#include "backlight_gpio.h"
#include "display_common.h"
#include "display_items.h"
#include "spi_display.h"
// if needed it can be lowered to 27000000, while maximum is 62.5 Mhz
#define SPI_CLOCK_HZ 40000000
#define SPI_MODE 0
#define CHAR_WIDTH 8
#define ST7789_SWRESET 0x01
#define ST7789_SLPIN 0x10
#define ST7789_SLPOUT 0x11
#define ST7789_NORON 0x13
#define ST7789_INVON 0x21
#define ST7789_DISPOFF 0x28
#define ST7789_DISPON 0x29
#define ST7789_CASET 0x2A
#define ST7789_RASET 0x2B
#define ST7789_RAMWR 0x2C
#define ST7789_MADCTL 0x36
#define ST7789_COLMOD 0x3A
#define ST7789_RAMCTRL 0xB0
#define ST7789_PORCTRL 0xB2
#define ST7789_GCTRL 0xB7
#define ST7789_VCOMS 0xBB
#define ST7789_LCMCTRL 0xC0
#define ST7789_VDVVRHEN 0xC2
#define ST7789_VRHS 0xC3
#define ST7789_VDVSET 0xC4
#define ST7789_FRCTR2 0xC6
#define ST7789_PWCTRL1 0xD0
#define ST7789_PVGAMCTRL 0xE0
#define ST7789_NVGAMCTRL 0xE1
// rotation
#define ST7789_MADCTL_MY 0x80
#define ST7789_MADCTL_MX 0x40
#define ST7789_MADCTL_MV 0x20
#define ST7789_MADCTL_ML 0x10
#define ST7789_MADCTL_RGB 0x00
#define TFT_MAD_RGB 0x00
#define TFT_MAD_BGR 0x08
#define TFT_MAD_COLOR_ORDER TFT_MAD_RGB
#include "font.c"
static const char *TAG = "st7789_display_driver";
static void send_message(term pid, term message, GlobalContext *global);
static inline void delay(int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
struct SPI
{
struct SPIDisplay spi_disp;
int dc_gpio;
int reset_gpio;
avm_int_t rotation;
Context *ctx;
};
// This struct is just for compatibility reasons with the SDL display driver
// so it is possible to easily copy & paste code from there.
struct Screen
{
int w;
int h;
uint16_t *pixels;
uint16_t *pixels_out;
};
static struct Screen *screen;
// This functions is taken from:
// https://stackoverflow.com/questions/18937701/combining-two-16-bits-rgb-colors-with-alpha-blending
static inline uint16_t alpha_blend_rgb565(uint32_t fg, uint32_t bg, uint8_t alpha)
{
alpha = (alpha + 4) >> 3;
bg = (bg | (bg << 16)) & 0b00000111111000001111100000011111;
fg = (fg | (fg << 16)) & 0b00000111111000001111100000011111;
uint32_t result = ((((fg - bg) * alpha) >> 5) + bg) & 0b00000111111000001111100000011111;
return (uint16_t)((result >> 16) | result);
}
static inline uint8_t rgba8888_get_alpha(uint32_t color)
{
return color & 0xFF;
}
static inline uint16_t rgba8888_color_to_rgb565(struct Screen *s, uint32_t color)
{
uint8_t r = color >> 24;
uint8_t g = (color >> 16) & 0xFF;
uint8_t b = (color >> 8) & 0xFF;
return (((uint16_t)(r >> 3)) << 11) | (((uint16_t)(g >> 2)) << 5) | ((uint16_t) b >> 3);
}
static inline uint16_t rgb565_color_to_surface(struct Screen *s, uint16_t color16)
{
return (uint16_t) SPI_SWAP_DATA_TX(color16, 16);
}
static inline uint16_t uint32_color_to_surface(struct Screen *s, uint32_t color)
{
uint16_t color16 = rgba8888_color_to_rgb565(s, color);
return rgb565_color_to_surface(s, color16);
}
struct PendingReply
{
uint64_t pending_call_ref_ticks;
term pending_call_pid;
};
static QueueHandle_t display_messages_queue;
static NativeHandlerResult display_driver_consume_mailbox(Context *ctx);
static void display_init(Context *ctx, term opts);
static void display_init_alt_gamma_2(struct SPI *spi);
static void display_init_std(struct SPI *spi);
static inline void writedata(struct SPI *spi, uint32_t data)
{
spi_device_acquire_bus(spi->spi_disp.handle, portMAX_DELAY);
spi_display_write(&spi->spi_disp, 8, data);
spi_device_release_bus(spi->spi_disp.handle);
}
static inline void writecommand(struct SPI *spi, uint8_t command)
{
gpio_set_level(spi->dc_gpio, 0);
writedata(spi, command);
gpio_set_level(spi->dc_gpio, 1);
}
static inline void set_screen_paint_area(struct SPI *spi, int x, int y, int width, int height)
{
writecommand(spi, ST7789_CASET);
spi_device_acquire_bus(spi->spi_disp.handle, portMAX_DELAY);
spi_display_write(&spi->spi_disp, 32, (x << 16) | ((x + width) - 1));
spi_device_release_bus(spi->spi_disp.handle);
writecommand(spi, ST7789_RASET);
spi_device_acquire_bus(spi->spi_disp.handle, portMAX_DELAY);
spi_display_write(&spi->spi_disp, 32, (y << 16) | ((y + height) - 1));
spi_device_release_bus(spi->spi_disp.handle);
}
static int draw_image_x(int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int y = item->y;
uint16_t bgcolor = 0;
bool visible_bg;
if (item->brcolor != 0) {
bgcolor = rgba8888_color_to_rgb565(screen, item->brcolor);
visible_bg = true;
} else {
visible_bg = false;
}
int width = item->width;
const char *data = item->data.image_data.pix;
int drawn_pixels = 0;
uint32_t *pixels = ((uint32_t *) data) + (ypos - y) * width + (xpos - x);
uint16_t *pixmem16 = (uint16_t *) (((uint8_t *) screen->pixels) + xpos * sizeof(uint16_t));
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
uint32_t img_pixel = READ_32_UNALIGNED(pixels);
uint8_t alpha = rgba8888_get_alpha(img_pixel);
if (alpha == 0xFF) {
uint16_t color = uint32_color_to_surface(screen, img_pixel);
pixmem16[drawn_pixels] = color;
} else if (visible_bg) {
uint16_t color = rgba8888_color_to_rgb565(screen, img_pixel);
uint16_t blended = alpha_blend_rgb565(color, bgcolor, alpha);
pixmem16[drawn_pixels] = rgb565_color_to_surface(screen, blended);
} else {
return drawn_pixels;
}
drawn_pixels++;
pixels++;
}
return drawn_pixels;
}
static int draw_scaled_cropped_img_x(int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int y = item->y;
uint16_t bgcolor = 0;
bool visible_bg;
if (item->brcolor != 0) {
bgcolor = rgba8888_color_to_rgb565(screen, item->brcolor);
visible_bg = true;
} else {
visible_bg = false;
}
int width = item->width;
const char *data = item->data.image_data_with_size.pix;
int drawn_pixels = 0;
int y_scale = item->y_scale;
int x_scale = item->x_scale;
int img_width = item->data.image_data_with_size.width;
int source_x = item->source_x;
int source_y = item->source_y;
uint32_t *pixels = ((uint32_t *) data) + (source_y + ((ypos - y) / y_scale)) * img_width + source_x + ((xpos - x) / x_scale);
uint16_t *pixmem16 = (uint16_t *) (((uint8_t *) screen->pixels) + xpos * sizeof(uint16_t));
if (source_x + (width / x_scale) > img_width) {
width = (img_width - source_x) * x_scale;
}
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
uint32_t img_pixel = READ_32_UNALIGNED(pixels);
uint8_t alpha = rgba8888_get_alpha(img_pixel);
if (alpha == 0xFF) {
uint16_t color = uint32_color_to_surface(screen, img_pixel);
pixmem16[drawn_pixels] = color;
} else if (visible_bg) {
uint16_t color = rgba8888_color_to_rgb565(screen, img_pixel);
uint16_t blended = alpha_blend_rgb565(color, bgcolor, alpha);
pixmem16[drawn_pixels] = rgb565_color_to_surface(screen, blended);
} else {
return drawn_pixels;
}
drawn_pixels++;
// TODO: optimize here
pixels = ((uint32_t *) data) + (source_y + ((ypos - y) / y_scale)) * img_width + source_x + (j / x_scale);
}
return drawn_pixels;
}
static int draw_rect_x(int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int width = item->width;
uint16_t color = uint32_color_to_surface(screen, item->brcolor);
int drawn_pixels = 0;
uint16_t *pixmem16 = (uint16_t *) (((uint8_t *) screen->pixels) + xpos * sizeof(uint16_t));
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
pixmem16[drawn_pixels] = color;
drawn_pixels++;
}
return drawn_pixels;
}
static int draw_text_x(int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int y = item->y;
uint16_t fgcolor = uint32_color_to_surface(screen, item->data.text_data.fgcolor);
uint16_t bgcolor;
bool visible_bg;
if (item->brcolor != 0) {
bgcolor = uint32_color_to_surface(screen, item->brcolor);
visible_bg = true;
} else {
visible_bg = false;
}
char *text = (char *) item->data.text_data.text;
int width = item->width;
int drawn_pixels = 0;
uint16_t *pixmem32 = (uint16_t *) (((uint8_t *) screen->pixels) + xpos * sizeof(uint16_t));
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
int char_index = j / CHAR_WIDTH;
char c = text[char_index];
unsigned const char *glyph = fontdata + ((unsigned char) c) * 16;
unsigned char row = glyph[ypos - y];
bool opaque;
int k = j % CHAR_WIDTH;
if (row & (1 << (7 - k))) {
opaque = true;
} else {
opaque = false;
}
if (opaque) {
pixmem32[drawn_pixels] = fgcolor;
} else if (visible_bg) {
pixmem32[drawn_pixels] = bgcolor;
} else {
return drawn_pixels;
}
drawn_pixels++;
}
return drawn_pixels;
}
static int find_max_line_len(BaseDisplayItem *items, int count, int xpos, int ypos)
{
int line_len = screen->w;
for (int i = 0; i < count; i++) {
BaseDisplayItem *item = &items[i];
if ((xpos < item->x) && (ypos >= item->y) && (ypos < item->y + item->height)) {
int len_to_item = item->x - xpos;
line_len = (line_len > len_to_item) ? len_to_item : line_len;
}
}
return line_len;
}
static int draw_x(int xpos, int ypos, BaseDisplayItem *items, int items_count)
{
bool below = false;
for (int i = 0; i < items_count; i++) {
BaseDisplayItem *item = &items[i];
if ((xpos < item->x) || (xpos >= item->x + item->width) || (ypos < item->y) || (ypos >= item->y + item->height)) {
continue;
}
int max_line_len = below ? 1 : find_max_line_len(items, i, xpos, ypos);
int drawn_pixels = 0;
switch (items[i].primitive) {
case Image:
drawn_pixels = draw_image_x(xpos, ypos, max_line_len, item);
break;
case Rect:
drawn_pixels = draw_rect_x(xpos, ypos, max_line_len, item);
break;
case ScaledCroppedImage:
drawn_pixels = draw_scaled_cropped_img_x(xpos, ypos, max_line_len, item);
break;
case Text:
drawn_pixels = draw_text_x(xpos, ypos, max_line_len, item);
break;
default: {
fprintf(stderr, "unexpected display list command.\n");
}
}
if (drawn_pixels != 0) {
return drawn_pixels;
}
below = true;
}
return 1;
}
static void do_update(Context *ctx, term display_list)
{
int proper;
int len = term_list_length(display_list, &proper);
BaseDisplayItem *items = malloc(sizeof(BaseDisplayItem) * len);
term t = display_list;
for (int i = 0; i < len; i++) {
init_item(&items[i], term_get_list_head(t), ctx);
t = term_get_list_tail(t);
}
int screen_width = screen->w;
int screen_height = screen->h;
struct SPI *spi = ctx->platform_data;
set_screen_paint_area(spi, 0, 0, screen_width, screen_height);
writecommand(spi, ST7789_RAMWR);
spi_device_acquire_bus(spi->spi_disp.handle, portMAX_DELAY);
bool transaction_in_progress = false;
for (int ypos = 0; ypos < screen_height; ypos++) {
int xpos = 0;
while (xpos < screen_width) {
int drawn_pixels = draw_x(xpos, ypos, items, len);
xpos += drawn_pixels;
}
if (transaction_in_progress) {
spi_transaction_t *trans;
// I did a quick measurement, and most of the time is spent waiting for DMA transaction
// eg. 23 us spent in draw_x, 188 us spent in spi_device_get_trans_result
spi_device_get_trans_result(spi->spi_disp.handle, &trans, portMAX_DELAY);
}
// NEW CODE
void *tmp = screen->pixels;
screen->pixels = screen->pixels_out;
screen->pixels_out = tmp;
spi_display_dmawrite(&spi->spi_disp, screen_width * sizeof(uint16_t), screen->pixels_out);
transaction_in_progress = true;
}
if (transaction_in_progress) {
spi_transaction_t *trans;
spi_device_get_trans_result(spi->spi_disp.handle, &trans, portMAX_DELAY);
}
spi_device_release_bus(spi->spi_disp.handle);
destroy_items(items, len);
}
static void draw_buffer(struct SPI *spi, int x, int y, int width, int height, const void *imgdata)
{
const uint16_t *data = imgdata;
set_screen_paint_area(spi, x, y, width, height);
writecommand(spi, ST7789_RAMWR);
int dest_size = width * height;
int buf_pixel_size = (dest_size > 1024) ? 1024 : dest_size;
int chunks = dest_size / 1024;
uint16_t *tmpbuf = heap_caps_malloc(buf_pixel_size * sizeof(uint16_t), MALLOC_CAP_DMA);
spi_device_acquire_bus(spi->spi_disp.handle, portMAX_DELAY);
for (int i = 0; i < chunks; i++) {
const uint16_t *data_b = data + 1024 * i;
for (int j = 0; j < 1024; j++) {
tmpbuf[j] = SPI_SWAP_DATA_TX(data_b[j], 16);
}
spi_display_dmawrite(&spi->spi_disp, buf_pixel_size * sizeof(uint16_t), tmpbuf);
}
int last_chunk_size = dest_size - chunks * 1024;
if (last_chunk_size) {
const uint16_t *data_b = data + chunks * 1024;
for (int j = 0; j < 1024; j++) {
tmpbuf[j] = SPI_SWAP_DATA_TX(data_b[j], 16);
}
spi_display_dmawrite(&spi->spi_disp, last_chunk_size * sizeof(uint16_t), tmpbuf);
}
spi_device_release_bus(spi->spi_disp.handle);
free(tmpbuf);
}
static void process_message(Message *message, Context *ctx)
{
GenMessage gen_message;
if (UNLIKELY(port_parse_gen_message(message->message, &gen_message) != GenCallMessage)) {
fprintf(stderr, "Received invalid message.");
AVM_ABORT();
}
term req = gen_message.req;
if (UNLIKELY(!term_is_tuple(req) || term_get_tuple_arity(req) < 1)) {
AVM_ABORT();
}
term cmd = term_get_tuple_element(req, 0);
struct SPI *spi = ctx->platform_data;
if (cmd == context_make_atom(ctx, "\x6"
"update")) {
term display_list = term_get_tuple_element(req, 1);
do_update(ctx, display_list);
} else if (cmd == context_make_atom(ctx, "\xB"
"draw_buffer")) {
int x = term_to_int(term_get_tuple_element(req, 1));
int y = term_to_int(term_get_tuple_element(req, 2));
int width = term_to_int(term_get_tuple_element(req, 3));
int height = term_to_int(term_get_tuple_element(req, 4));
unsigned long addr_low = term_to_int(term_get_tuple_element(req, 5));
unsigned long addr_high = term_to_int(term_get_tuple_element(req, 6));
const void *data = (const void *) ((addr_low | (addr_high << 16)));
draw_buffer(spi, x, y, width, height, data);
// draw_buffer is a kind of cast, no need to reply
return;
} else {
fprintf(stderr, "display: ");
term_display(stderr, req, ctx);
fprintf(stderr, "\n");
}
BEGIN_WITH_STACK_HEAP(TUPLE_SIZE(2) + REF_SIZE, heap);
term return_tuple = term_alloc_tuple(2, &heap);
term_put_tuple_element(return_tuple, 0, gen_message.ref);
term_put_tuple_element(return_tuple, 1, OK_ATOM);
send_message(gen_message.pid, return_tuple, ctx->global);
END_WITH_STACK_HEAP(heap, ctx->global);
}
static void process_messages(void *arg)
{
struct SPI *args = arg;
while (true) {
Message *message;
xQueueReceive(display_messages_queue, &message, portMAX_DELAY);
process_message(message, args->ctx);
BEGIN_WITH_STACK_HEAP(1, temp_heap);
mailbox_message_dispose(&message->base, &temp_heap);
END_WITH_STACK_HEAP(temp_heap, args->ctx->global);
}
}
static NativeHandlerResult display_driver_consume_mailbox(Context *ctx)
{
MailboxMessage *mbox_msg = mailbox_take_message(&ctx->mailbox);
Message *msg = CONTAINER_OF(mbox_msg, Message, base);
xQueueSend(display_messages_queue, &msg, 1);
return NativeContinue;
}
static void set_rotation(struct SPI *spi, int rotation)
{
if (rotation == 1) {
writecommand(spi, ST7789_MADCTL);
writedata(spi, ST7789_MADCTL_MX | ST7789_MADCTL_MV | ST7789_MADCTL_RGB);
}
}
Context *st7789_display_create_port(GlobalContext *global, term opts)
{
Context *ctx = context_new(global);
ctx->native_handler = display_driver_consume_mailbox;
display_init(ctx, opts);
return ctx;
}
static void send_message(term pid, term message, GlobalContext *global)
{
int local_process_id = term_to_local_process_id(pid);
globalcontext_send_message(global, local_process_id, message);
}
static void display_init(Context *ctx, term opts)
{
screen = malloc(sizeof(struct Screen));
// FIXME: hardcoded width and height
screen->w = 320;
screen->h = 240;
screen->pixels = heap_caps_malloc(screen->w * sizeof(uint16_t), MALLOC_CAP_DMA);
screen->pixels_out = heap_caps_malloc(screen->w * sizeof(uint16_t), MALLOC_CAP_DMA);
display_messages_queue = xQueueCreate(32, sizeof(Message *));
struct SPI *spi = malloc(sizeof(struct SPI));
ctx->platform_data = spi;
spi->ctx = ctx;
struct SPIDisplayConfig spi_config;
spi_display_init_config(&spi_config);
spi_config.mode = SPI_MODE;
spi_config.clock_speed_hz = SPI_CLOCK_HZ;
spi_display_parse_config(&spi_config, opts, ctx->global);
spi_display_init(&spi->spi_disp, &spi_config);
bool ok = display_common_gpio_from_opts(opts, ATOM_STR("\x2", "dc"), &spi->dc_gpio, ctx->global);
bool reset_configured = true;
if (!display_common_gpio_from_opts(opts, ATOM_STR("\x5", "reset"), &spi->reset_gpio, ctx->global)) {
ESP_LOGI(TAG, "Reset GPIO not configured.");
reset_configured = false;
}
term rotation = interop_kv_get_value_default(opts, ATOM_STR("\x8", "rotation"), term_from_int(0), ctx->global);
ok = ok && term_is_integer(rotation);
spi->rotation = term_to_int(rotation);
term invon = interop_kv_get_value_default(opts, ATOM_STR("\x10", "enable_tft_invon"), FALSE_ATOM, ctx->global);
ok = ok && ((invon == TRUE_ATOM) || (invon == FALSE_ATOM));
bool enable_tft_invon = (invon == TRUE_ATOM);
if (UNLIKELY(!ok)) {
ESP_LOGE(TAG, "Failed init: invalid display parameters.");
return;
}
// Reset
if (reset_configured) {
spi_device_acquire_bus(spi->spi_disp.handle, portMAX_DELAY);
gpio_set_direction(spi->reset_gpio, GPIO_MODE_OUTPUT);
gpio_set_level(spi->reset_gpio, 1);
vTaskDelay(50 / portTICK_PERIOD_MS);
gpio_set_level(spi->reset_gpio, 0);
vTaskDelay(50 / portTICK_PERIOD_MS);
gpio_set_level(spi->reset_gpio, 1);
spi_device_release_bus(spi->spi_disp.handle);
}
gpio_set_direction(spi->dc_gpio, GPIO_MODE_OUTPUT);
if (!reset_configured) {
writecommand(spi, ST7789_SWRESET);
delay(100);
}
term init_seq_type_term = interop_kv_get_value_default(opts, ATOM_STR("\xD", "init_seq_type"), term_nil(), ctx->global);
int str_ok;
char *init_seq_type_string = interop_term_to_string(init_seq_type_term, &str_ok);
if (str_ok && !strcmp(init_seq_type_string, "alt_gamma_2")) {
display_init_alt_gamma_2(spi);
free(init_seq_type_string);
} else {
display_init_std(spi);
}
set_rotation(spi, spi->rotation);
if (enable_tft_invon) {
writecommand(spi, ST7789_INVON);
}
writecommand(spi, ST7789_DISPON);
delay(120);
struct BacklightGPIOConfig backlight_config;
backlight_gpio_init_config(&backlight_config);
backlight_gpio_parse_config(&backlight_config, opts, ctx->global);
backlight_gpio_init(&backlight_config);
xTaskCreate(process_messages, "display", 10000, spi, 1, NULL);
}
static void display_init_alt_gamma_2(struct SPI *spi)
{
writecommand(spi, ST7789_SLPOUT);
delay(120);
writecommand(spi, ST7789_NORON);
// - display and color format setting - //
writecommand(spi, ST7789_MADCTL);
writedata(spi, TFT_MAD_COLOR_ORDER);
writecommand(spi, ST7789_COLMOD);
writedata(spi, 0x55);
delay(10);
// - ST7789V frame rate setting - //
writecommand(spi, ST7789_PORCTRL);
writedata(spi, 0x0C);
writedata(spi, 0x0C);
writedata(spi, 0x00);
writedata(spi, 0x33);
writedata(spi, 0x33);
writecommand(spi, ST7789_GCTRL);
writedata(spi, 0x75);
// - ST7789V power setting - //
writecommand(spi, ST7789_VCOMS);
writedata(spi, 0x1A);
writecommand(spi, ST7789_LCMCTRL);
writedata(spi, 0x2C);
writecommand(spi, ST7789_VDVVRHEN);
writedata(spi, 0x01);
writecommand(spi, ST7789_VRHS);
writedata(spi, 0x13);
writecommand(spi, ST7789_VDVSET);
writedata(spi, 0x20);
writecommand(spi, ST7789_FRCTR2);
writedata(spi, 0x0F);
writecommand(spi, ST7789_PWCTRL1);
writedata(spi, 0xA4);
writedata(spi, 0xA1);
// - ST7789V gamma setting - //
writecommand(spi, ST7789_PVGAMCTRL);
writedata(spi, 0xD0);
writedata(spi, 0x0D);
writedata(spi, 0x14);
writedata(spi, 0x0D);
writedata(spi, 0x0D);
writedata(spi, 0x09);
writedata(spi, 0x38);
writedata(spi, 0x44);
writedata(spi, 0x4E);
writedata(spi, 0x3A);
writedata(spi, 0x17);
writedata(spi, 0x18);
writedata(spi, 0x2F);
writedata(spi, 0x30);
writecommand(spi, ST7789_NVGAMCTRL);
writedata(spi, 0xD0);
writedata(spi, 0x09);
writedata(spi, 0x0F);
writedata(spi, 0x08);
writedata(spi, 0x07);
writedata(spi, 0x14);
writedata(spi, 0x37);
writedata(spi, 0x44);
writedata(spi, 0x4D);
writedata(spi, 0x38);
writedata(spi, 0x15);
writedata(spi, 0x16);
writedata(spi, 0x2C);
writedata(spi, 0x3E);
writecommand(spi, ST7789_CASET);
writedata(spi, 0x00);
writedata(spi, 0x00);
writedata(spi, 0x00);
writedata(spi, 0xEF); // 239
writecommand(spi, ST7789_RASET);
writedata(spi, 0x00);
writedata(spi, 0x00);
writedata(spi, 0x01);
writedata(spi, 0x3F); // 319
}
static void display_init_std(struct SPI *spi)
{
writecommand(spi, ST7789_SLPOUT);
delay(120);
writecommand(spi, ST7789_NORON);
// - display and color format setting - //
writecommand(spi, ST7789_MADCTL);
writedata(spi, TFT_MAD_COLOR_ORDER);
writecommand(spi, 0xB6);
writedata(spi, 0x0A);
writedata(spi, 0x82);
writecommand(spi, ST7789_RAMCTRL);
writedata(spi, 0x00);
writedata(spi, 0xE0);
writecommand(spi, ST7789_COLMOD);
writedata(spi, 0x55);
delay(10);
// - ST7789V frame rate setting - //
writecommand(spi, ST7789_PORCTRL);
writedata(spi, 0x0C);
writedata(spi, 0x0C);
writedata(spi, 0x00);
writedata(spi, 0x33);
writedata(spi, 0x33);
writecommand(spi, ST7789_GCTRL);
writedata(spi, 0x35);
// - ST7789V power setting - //
writecommand(spi, ST7789_VCOMS);
writedata(spi, 0x28);
writecommand(spi, ST7789_LCMCTRL);
writedata(spi, 0x0C);
writecommand(spi, ST7789_VDVVRHEN);
writedata(spi, 0x01);
writedata(spi, 0xFF);
writecommand(spi, ST7789_VRHS);
writedata(spi, 0x10);
writecommand(spi, ST7789_VDVSET);
writedata(spi, 0x20);
writecommand(spi, ST7789_FRCTR2);
writedata(spi, 0x0F);
writecommand(spi, ST7789_PWCTRL1);
writedata(spi, 0xA4);
writedata(spi, 0xA1);
// - ST7789V gamma setting - //
writecommand(spi, ST7789_PVGAMCTRL);
writedata(spi, 0xD0);
writedata(spi, 0x00);
writedata(spi, 0x02);
writedata(spi, 0x07);
writedata(spi, 0x0A);
writedata(spi, 0x28);
writedata(spi, 0x32);
writedata(spi, 0x44);
writedata(spi, 0x42);
writedata(spi, 0x06);
writedata(spi, 0x0E);
writedata(spi, 0x12);
writedata(spi, 0x14);
writedata(spi, 0x17);
writecommand(spi, ST7789_NVGAMCTRL);
writedata(spi, 0xD0);
writedata(spi, 0x00);
writedata(spi, 0x02);
writedata(spi, 0x07);
writedata(spi, 0x0A);
writedata(spi, 0x28);
writedata(spi, 0x31);
writedata(spi, 0x54);
writedata(spi, 0x47);
writedata(spi, 0x0E);
writedata(spi, 0x1C);
writedata(spi, 0x17);
writedata(spi, 0x1B);
writedata(spi, 0x1E);
writecommand(spi, ST7789_CASET);
writedata(spi, 0x00);
writedata(spi, 0x00);
writedata(spi, 0x00);
writedata(spi, 0xEF); // 239
writecommand(spi, ST7789_RASET);
writedata(spi, 0x00);
writedata(spi, 0x00);
writedata(spi, 0x01);
writedata(spi, 0x3F); // 319
}