-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFT_io.py
785 lines (697 loc) · 21.5 KB
/
TFT_io.py
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
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Robert Hammelrath
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Low level I/O drivers for the class supporting TFT LC-displays
# with a parallel Interface
# First example: Controller SSD1963
# It uses X1..X8 for data and Y3, Y9, Y10, Y11 and Y12 for control signals.
# The minimal connection is:
# X1..X8 for data, Y9 for /Reset, Y10 for /RD, Y11 for /WR and Y12 for /RS
# Then LED must be hard tied to Vcc and /CS to GND.
#
import pyb, stm
from uctypes import addressof
# define constants
#
RESET = const(1 << 10) ## Y9
RD = const(1 << 11) ## Y10
WR = const(0x01) ## Y11
D_C = const(0x02) ## Y12
LED = const(1 << 8) ## Y3
POWER = const(1 << 9) ## Y4
## CS is not used and must be hard tied to GND
PORTRAIT = const(1)
LANDSCAPE = const(0)
#
# display font bitmap for text
#
@micropython.viper
def displaySCR_charbitmap(bits: ptr8, size: int, control: ptr8, bg_buf: ptr8):
gpioa = ptr8(stm.GPIOA)
gpiob = ptr16(stm.GPIOB + stm.GPIO_BSRR)
#
transparency = control[6]
bm_ptr = 0
bg_ptr = 0
mask = 0x80
#
while size:
if bits[bm_ptr] & mask:
if transparency & 8: # Invert bg color as foreground
gpioa[stm.GPIO_ODR] = 255 - bg_buf[bg_ptr] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = 255 - bg_buf[bg_ptr + 1] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = 255 - bg_buf[bg_ptr + 2] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
else: # not invert
gpioa[stm.GPIO_ODR] = control[3] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = control[4] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = control[5] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
else:
if transparency & 1: # Dim background
gpioa[stm.GPIO_ODR] = bg_buf[bg_ptr] >> 1 # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = bg_buf[bg_ptr + 1] >> 1 # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = bg_buf[bg_ptr + 2] >> 1 # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
elif transparency & 2: # keep Background
gpioa[stm.GPIO_ODR] = bg_buf[bg_ptr] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = bg_buf[bg_ptr + 1] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = bg_buf[bg_ptr + 2] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
elif transparency & 4: # invert Background
gpioa[stm.GPIO_ODR] = 255 - bg_buf[bg_ptr] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = 255 - bg_buf[bg_ptr + 1] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = 255 - bg_buf[bg_ptr + 2] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
else: # not transparent
gpioa[stm.GPIO_ODR] = control[0] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = control[1] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = control[2] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
mask >>= 1
if mask == 0: # mask reset & data ptr advance on byte exhaust
mask = 0x80
bm_ptr += 1
size -= 1
bg_ptr += 3
#
# display Windows BMP data, optionally with colortables
#
@micropython.viper
def displaySCR_bmp(data: ptr8, size: int, bits: int, colortable: ptr8):
gpioa = ptr8(stm.GPIOA)
gpiob = ptr16(stm.GPIOB + stm.GPIO_BSRR)
#
bm_ptr = 0
shift = 8 - bits
mask = ((1 << bits) - 1) << shift
#
while size:
offset = ((data[bm_ptr] & mask) >> shift) * 4
gpioa[stm.GPIO_ODR] = colortable[offset + 2] # Red
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = colortable[offset + 1] # green
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
gpioa[stm.GPIO_ODR] = colortable[offset + 0] # blue
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
mask >>= bits
shift -= bits
if mask == 0: # mask rebuild & data ptr advance on byte exhaust
shift = 8 - bits
mask = ((1 << bits) - 1) << shift
bm_ptr += 1
size -= 1
#
# Set the address range for various draw commands and set the TFT for expecting data
#
#
# Assembler version of
# SetXY: takes net about 6 µs including the call
#
@micropython.asm_thumb
def setXY_L(r0, r1, r2, r3):
# r0: x1, r1: y1, r2: x2, r3: y2
# set up pointers to GPIO
# r4: changing data
# r5: bit mask for control lines
# r6: GPIOA ODR register ptr
# r7: GPIOB BSSRL register ptr
movwt(r6, stm.GPIOA) # target
add (r6, stm.GPIO_ODR)
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
b(start)
#inline subroutine
label(send)
strb(r4, [r6, 0]) # set command byte
strh(r5, [r7, 2]) # WR and D_C low
strh(r5, [r7, 0]) # WR and D_C high
bx(lr)
# Emit command byte
label(start)
movw(r5, WR | D_C)
mov (r4, 0x2a)
bl(send)
mov(r5, 8)
mov(r4, r0) # get x1
asr(r4, r5) # get the upper byte
mov(r5, WR)
bl(send)
mov(r4, r0)
bl(send)
mov(r0, 8) # from here on r0 keeps 8
mov(r4, r2) # get x2
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r2)
bl(send)
# Emit command byte
movw(r5, WR | D_C)
mov (r4, 0x2b)
bl(send)
mov(r5, WR)
mov(r4, r1) # get y1
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r1)
bl(send)
mov(r4, r3) # get x2
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r3)
bl(send)
# Emit command byte
movw(r5, WR | D_C)
mov (r4, 0x2c)
bl(send)
# and done
@micropython.asm_thumb
def setXY_P(r0, r1, r2, r3):
# r0: x1, r1: y1, r2: x2, r3: y2
# set up pointers to GPIO
# r4: changing data
# r5: bit mask for control lines
# r6: GPIOA ODR register ptr
# r7: GPIOB BSSRL register ptr
movwt(r6, stm.GPIOA) # target
add (r6, stm.GPIO_ODR)
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
b(start)
#inline subroutine
label(send)
strb(r4, [r6, 0]) # set command byte
strh(r5, [r7, 2]) # WR and D_C low
strh(r5, [r7, 0]) # WR and D_C high
bx(lr)
# Emit command byte
label(start)
movw(r5, WR | D_C)
mov (r4, 0x2b)
bl(send)
mov(r5, 8)
mov(r4, r0) # get x1
asr(r4, r5) # get the upper byte
mov(r5, WR)
bl(send)
mov(r4, r0)
bl(send)
mov(r0, 8) # from here on r0 keeps 8
mov(r4, r2) # get x2
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r2)
bl(send)
# Emit command byte
movw(r5, WR | D_C)
mov (r4, 0x2a)
bl(send)
mov(r5, WR)
mov(r4, r1) # get y1
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r1)
bl(send)
mov(r4, r3) # get x2
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r3)
bl(send)
# Emit command byte
movw(r5, WR | D_C)
mov (r4, 0x2c)
bl(send)
# and done
#
# Assembler version of
# drawPixel, Landscape
#
@micropython.asm_thumb
def drawPixel_L(r0, r1, r2):
# r0: x, r1: y, r2: colorvector
# set up pointers to GPIO
# r4: changing data
# r5: bit mask for control lines
# r6: GPIOA ODR register ptr
# r7: GPIOB BSSRL register ptr
movwt(r6, stm.GPIOA) # target
add (r6, stm.GPIO_ODR)
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
b(start)
#inline subroutine
label(send)
strb(r4, [r6, 0]) # set command byte
strh(r5, [r7, 2]) # WR and D_C low
strh(r5, [r7, 0]) # WR and D_C high
bx(lr)
# Emit command byte
label(start)
movw(r5, WR | D_C)
mov (r4, 0x2a)
bl(send)
mov(r5, 8)
mov(r4, r0) # get x1
asr(r4, r5) # get the upper byte
mov(r5, WR)
bl(send)
mov(r4, r0)
bl(send)
mov(r0, 8) # from here on r0 keeps 8
mov(r4, r2) # get x2
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r2)
bl(send)
# Emit command byte
movw(r5, WR | D_C)
mov (r4, 0x2b)
bl(send)
mov(r5, WR)
mov(r4, r1) # get y1
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r1)
bl(send)
mov(r4, r3) # get x2
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r3)
bl(send)
# Emit command byte
movw(r5, WR | D_C)
mov (r4, 0x2c)
bl(send)
# Send color
mov(r5, WR)
ldrb(r4, [r2, 0]) # red
bl(send)
ldrb(r4, [r2, 1]) # green
bl(send)
ldrb(r4, [r2, 2]) # blue
bl(send)
# and done
#
# Assembler version of
# drawPixel, Portrait
#
@micropython.asm_thumb
def drawPixel_P(r0, r1, r2):
# r0: x, r1: y, r2: colorvector
# set up pointers to GPIO
# r4: changing data
# r5: bit mask for control lines
# r6: GPIOA ODR register ptr
# r7: GPIOB BSSRL register ptr
movwt(r6, stm.GPIOA) # target
add (r6, stm.GPIO_ODR)
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
b(start)
#inline subroutine
label(send)
strb(r4, [r6, 0]) # set command byte
strh(r5, [r7, 2]) # WR and D_C low
strh(r5, [r7, 0]) # WR and D_C high
bx(lr)
# Emit command byte
label(start)
movw(r5, WR | D_C)
mov (r4, 0x2b)
bl(send)
mov(r5, 8)
mov(r4, r0) # get x1
asr(r4, r5) # get the upper byte
mov(r5, WR)
bl(send)
mov(r4, r0)
bl(send)
mov(r0, 8) # from here on r0 keeps 8
mov(r4, r2) # get x2
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r2)
bl(send)
# Emit command byte
movw(r5, WR | D_C)
mov (r4, 0x2a)
bl(send)
mov(r5, WR)
mov(r4, r1) # get y1
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r1)
bl(send)
mov(r4, r3) # get x2
asr(r4, r0) # get the upper byte
bl(send)
mov(r4, r3)
bl(send)
# Emit command byte
movw(r5, WR | D_C)
mov (r4, 0x2c)
bl(send)
# Send color
mov(r5, WR)
ldrb(r4, [r2, 0]) # red
bl(send)
ldrb(r4, [r2, 1]) # green
bl(send)
ldrb(r4, [r2, 2]) # blue
bl(send)
# Assembler version of
# Fill screen by writing size pixels with the color given in data
# data must be 3 bytes of red, green, blue
# The area to be filled has to be set in advance by setXY
# The speed is about 214 ns/pixel
#
@micropython.asm_thumb
def fillSCR_AS(r0, r1): # r0: ptr to data, r1: number of pixels (3 bytes/pixel)
# set up pointers to GPIO
# r5: bit mask for control lines
# r6: GPIOA ODR register ptr
# r7: GPIOB BSSRL register ptr
mov(r5, WR)
movwt(r6, stm.GPIOA) # target
add (r6, stm.GPIO_ODR)
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
ldrb(r2, [r0, 0]) # red
ldrb(r3, [r0, 1]) # green
ldrb(r4, [r0, 2]) # blue
b(loopend)
label(loopstart)
strb(r2, [r6, 0]) # Store red
strb(r5, [r7, 2]) # WR low
# nop()
strb(r5, [r7, 0]) # WR high
strb(r3, [r6, 0]) # store blue
strb(r5, [r7, 2]) # WR low
nop()
strb(r5, [r7, 0]) # WR high
strb(r4, [r6, 0]) # store blue
strb(r5, [r7, 2]) # WR low
# nop()
strb(r5, [r7, 0]) # WR high
label(loopend)
sub (r1, 1) # End of loop?
bpl(loopstart)
#
# Assembler version of:
# Fill screen by writing size pixels with the data
# data must contains size triplets of red, green and blue data values
# The area to be filled has to be set in advance by setXY
# the speed is 266 ns for a byte triple
#
@micropython.asm_thumb
def displaySCR_AS(r0, r1): # r0: ptr to data, r1: is number of pixels (3 bytes/pixel)
# Color oder is blue-gree-red
# set up pointers to GPIO
# r5: bit mask for control lines
# r6: GPIOA ODR register ptr
# r7: GPIOB BSSRL register ptr
mov(r5, WR)
movwt(r6, stm.GPIOA) # target
add (r6, stm.GPIO_ODR)
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
b(loopend)
label(loopstart)
ldrb(r2, [r0, 2]) # red
strb(r2, [r6, 0]) # Store red
strb(r5, [r7, 2]) # WR low
strb(r5, [r7, 0]) # WR high
ldrb(r2, [r0, 1]) # pre green
strb(r2, [r6, 0]) # store greem
strb(r5, [r7, 2]) # WR low
strb(r5, [r7, 0]) # WR high
ldrb(r2, [r0, 0]) # blue
strb(r2, [r6, 0]) # store blue
strb(r5, [r7, 2]) # WR low
strb(r5, [r7, 0]) # WR high
add (r0, 3) # advance data ptr
label(loopend)
sub (r1, 1) # End of loop?
bpl(loopstart)
# Assembler version of:
# Fill screen by writing size pixels with the data
# data must contains size packed duplets of red, green and blue data values
# The area to be filled has to be set in advance by setXY
# the speed is 266 ns for a byte pixel
#
@micropython.asm_thumb
def displaySCR565_AS(r0, r1): # r0: ptr to data, r1: is number of pixels (3 bytes/pixel)
# Color oder is blue-gree-red
# set up pointers to GPIO
# r5: bit mask for control lines
# r6: GPIOA ODR register ptr
# r7: GPIOB BSSRL register ptr
mov(r5, WR)
movwt(r6, stm.GPIOA) # target
add (r6, stm.GPIO_ODR)
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
b(loopend)
label(loopstart)
ldrb(r2, [r0, 1]) # red
mov (r3, 0xf8) # mask out lower 3 bits
and_(r2, r3)
strb(r2, [r6, 0]) # Store red
strb(r5, [r7, 2]) # WR low
strb(r5, [r7, 0]) # WR high
ldrb(r2, [r0, 1]) # pre green
mov (r3, 5) # shift 5 bits up to
lsl(r2, r3)
ldrb(r4, [r0, 0]) # get the next 3 bits
mov (r3, 3) # shift 3 to the right
lsr(r4, r3)
orr(r2, r4) # add them to the first bits
mov(r3, 0xfc) # mask off the lower two bits
and_(r2, r3)
strb(r2, [r6, 0]) # store green
strb(r5, [r7, 2]) # WR low
strb(r5, [r7, 0]) # WR high
ldrb(r2, [r0, 0]) # blue
mov (r3, 3)
lsl(r2, r3)
strb(r2, [r6, 0]) # store blue
strb(r5, [r7, 2]) # WR low
strb(r5, [r7, 0]) # WR high
add (r0, 2) # advance data ptr
label(loopend)
sub (r1, 1) # End of loop?
bpl(loopstart)
#
# Send a command and data to the TFT controller
# cmd is the command byte, data must be a bytearray object with the command payload,
# int is the size of the data
# For the startup-phase use this function.
#
@micropython.viper
def tft_cmd_data(cmd: int, data: ptr8, size: int):
gpioa = ptr8(stm.GPIOA + stm.GPIO_ODR)
gpiob = ptr16(stm.GPIOB + stm.GPIO_BSRR)
gpioa[0] = cmd # set data on port A
gpiob[1] = D_C | WR # set C/D and WR low
gpiob[0] = D_C | WR # set C/D and WR high
for i in range(size):
gpioa[0] = data[i] # set data on port A
gpiob[1] = WR # set WR low. C/D still high
gpiob[0] = WR # set WR high again
#
# Assembler version of send command & data to the TFT controller
# data must be a bytearray object, int is the size of the data.
# The speed is about 120 ns/byte
#
@micropython.asm_thumb
def tft_cmd_data_AS(r0, r1, r2): # r0: command, r1: ptr to data, r2 is size in bytes
# set up pointers to GPIO
# r5: bit mask for control lines
# r6: GPIOA ODR register ptr
# r7: GPIOB BSSRL register ptr
movwt(r6, stm.GPIOA) # target
add (r6, stm.GPIO_ODR)
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
# Emit command byte
mov(r5, WR | D_C)
strb(r0, [r6, 0]) # set command byte
strh(r5, [r7, 2]) # WR and D_C low
strh(r5, [r7, 0]) # WR and D_C high
# now loop though data
mov(r5, WR)
b(loopend)
label(loopstart)
ldrb(r4, [r1, 0]) # load data
strb(r4, [r6, 0]) # Store data
strh(r5, [r7, 2]) # WR low
strh(r5, [r7, 0]) # WR high
add (r1, 1) # advance data ptr
label(loopend)
sub (r2, 1) # End of loop?
bpl(loopstart)
#
# Send a command to the TFT controller
#
@micropython.viper
def tft_cmd(cmd: int):
gpioa = ptr8(stm.GPIOA + stm.GPIO_ODR)
gpiob = ptr16(stm.GPIOB + stm.GPIO_BSRR)
gpioa[0] = cmd # set data on port A
gpiob[1] = D_C | WR # set C/D and WR low
gpiob[0] = D_C | WR # set C/D and WR high
#
# Assembler version of send data to the TFT controller
# data must be a bytearray object, int is the size of the data.
# The speed is about 120 ns/byte
#
@micropython.asm_thumb
def tft_write_data_AS(r0, r1): # r0: ptr to data, r1: is size in Bytes
# set up pointers to GPIO
# r5: bit mask for control lines
# r6: GPIOA ODR register ptr
# r7: GPIOB BSSRL register ptr
movwt(r6, stm.GPIOA) # target
add (r6, stm.GPIO_ODR)
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
mov(r5, WR)
# and go, first test size for 0
b(loopend)
label(loopstart)
ldrb(r3, [r0, 0]) # load data
strb(r3, [r6, 0]) # Store data
strh(r5, [r7, 2]) # WR low
strh(r5, [r7, 0]) # WR high
add (r0, 1) # advance data ptr
label(loopend)
sub (r1, 1) # End of loop?
bpl(loopstart)
#
# Assembler version of send a command byte and read data from to the TFT controller
# data must be a bytearray object, int is the size of the data.
# The speed is about 130 ns/byte
#
@micropython.asm_thumb
def tft_read_cmd_data_AS(r0, r1, r2):
# r0: command, r1: ptr to data buffer, r2 is expected size in bytes
# set up pointers to GPIO
# r5: bit mask for control lines
# r6: GPIOA base register ptr
# r7: GPIOB BSSRL register ptr
movwt(r6, stm.GPIOA) # target
movwt(r7, stm.GPIOB)
add (r7, stm.GPIO_BSRR)
# Emit command byte
movw(r5, WR | D_C)
strb(r0, [r6, stm.GPIO_ODR]) # set command byte
strh(r5, [r7, 2]) # WR and D_C low
strh(r5, [r7, 0]) # WR and D_C high
# now switch gpioaa to input
movw(r0, 0)
strh(r0, [r6, stm.GPIO_MODER])
# now loop though data
movw(r5, RD)
b(loopend)
label(loopstart)
strh(r5, [r7, 2]) # RD low
nop() # short delay
nop()
ldrb(r4, [r6, stm.GPIO_IDR]) # load data
strh(r5, [r7, 0]) # RD high
strb(r4, [r1, 0]) # Store data
add (r1, 1) # advance data ptr
label(loopend)
sub (r2, 1) # End of loop?
bpl(loopstart)
# now switch gpioaa back to input
movw(r0, 0x5555)
strh(r0, [r6, stm.GPIO_MODER])
#
# swap byte pairs in a buffer
# sometimes needed for picture data
#
@micropython.asm_thumb
def swapbytes(r0, r1): # bytearray, len(bytearray)
mov(r2, 1) # divide loop count by 2
lsr(r1, r2) # to avoid odd valued counter
b(loopend)
label(loopstart)
ldrb(r2, [r0, 0])
ldrb(r3, [r0, 1])
strb(r3, [r0, 0])
strb(r2, [r0, 1])
add(r0, 2)
label(loopend)
sub (r1, 1) # End of loop?
bpl(loopstart)
#
# swap colors red/blue in the buffer
#
@micropython.asm_thumb
def swapcolors(r0, r1): # bytearray, len(bytearray)
mov(r2, 3)
udiv(r1, r1, r2) # 3 bytes per triple
b(loopend)
label(loopstart)
ldrb(r2, [r0, 0])
ldrb(r3, [r0, 2])
strb(r3, [r0, 0])
strb(r2, [r0, 2])
add(r0, 3)
label(loopend)
sub (r1, 1) # End of loop?
bpl(loopstart)