-
Notifications
You must be signed in to change notification settings - Fork 122
/
U8x8lib.cpp
1910 lines (1572 loc) · 55.2 KB
/
U8x8lib.cpp
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
U8x8lib.cpp
Arduino specific low level functions
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2016, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "U8x8lib.h"
#ifdef ARDUINO
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
# ifdef U8X8_HAVE_HW_I2C_TEENSY3
# include <i2c_t3.h>
# else
# include <Wire.h>
# ifdef U8X8_HAVE_2ND_HW_I2C
# if defined(MINICORE) && defined(__AVR_ATmega328PB__)
# include <Wire1.h>
# endif
# endif
# endif
#endif /* U8X8_HAVE_HW_I2C */
#endif /* ARDUINO */
/*=============================================*/
size_t U8X8::write(uint8_t v)
{
if ( v == '\n' )
{
uint8_t dy = u8x8_pgm_read(u8x8.font+3); /* new 2019 format */
ty+=dy;
tx=0;
}
else
{
uint8_t dx = u8x8_pgm_read(u8x8.font+2); /* new 2019 format */
u8x8_DrawGlyph(&u8x8, tx, ty, v);
tx+=dx;
}
return 1;
}
/*=============================================*/
/*=== ARDUINO GPIO & DELAY ===*/
#ifdef ARDUINO
#ifdef U8X8_USE_PINS
extern "C" uint8_t u8x8_gpio_and_delay_arduino(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
{
uint8_t i;
switch(msg)
{
case U8X8_MSG_GPIO_AND_DELAY_INIT:
for( i = 0; i < U8X8_PIN_CNT; i++ )
if ( u8x8->pins[i] != U8X8_PIN_NONE )
{
if ( i < U8X8_PIN_OUTPUT_CNT )
{
pinMode(u8x8->pins[i], OUTPUT);
}
else
{
#ifdef INPUT_PULLUP
pinMode(u8x8->pins[i], INPUT_PULLUP);
#else
pinMode(u8x8->pins[i], OUTPUT);
digitalWrite(u8x8->pins[i], 1);
#endif
}
}
break;
#ifndef __AVR__
/* this case is not compiled for any AVR, because AVR uC are so slow */
/* that this delay does not matter */
case U8X8_MSG_DELAY_NANO:
delayMicroseconds(arg_int==0?0:1);
break;
#endif
case U8X8_MSG_DELAY_10MICRO:
/* not used at the moment */
break;
case U8X8_MSG_DELAY_100NANO:
/* not used at the moment */
break;
case U8X8_MSG_DELAY_MILLI:
delay(arg_int);
break;
case U8X8_MSG_DELAY_I2C:
/* arg_int is 1 or 4: 100KHz (5us) or 400KHz (1.25us) */
delayMicroseconds(arg_int<=2?5:2);
break;
case U8X8_MSG_GPIO_I2C_CLOCK:
case U8X8_MSG_GPIO_I2C_DATA:
if ( arg_int == 0 )
{
pinMode(u8x8_GetPinValue(u8x8, msg), OUTPUT);
digitalWrite(u8x8_GetPinValue(u8x8, msg), 0);
}
else
{
#ifdef INPUT_PULLUP
pinMode(u8x8_GetPinValue(u8x8, msg), INPUT_PULLUP);
#else
pinMode(u8x8_GetPinValue(u8x8, msg), OUTPUT);
digitalWrite(u8x8_GetPinValue(u8x8, msg), 1);
#endif
}
break;
default:
if ( msg >= U8X8_MSG_GPIO(0) )
{
i = u8x8_GetPinValue(u8x8, msg);
if ( i != U8X8_PIN_NONE )
{
if ( u8x8_GetPinIndex(u8x8, msg) < U8X8_PIN_OUTPUT_CNT )
{
digitalWrite(i, arg_int);
}
else
{
if ( u8x8_GetPinIndex(u8x8, msg) == U8X8_PIN_OUTPUT_CNT )
{
// call yield() for the first pin only, u8x8 will always request all the pins, so this should be ok
yield();
}
u8x8_SetGPIOResult(u8x8, digitalRead(i) == 0 ? 0 : 1);
}
}
break;
}
return 0;
}
return 1;
}
#endif // U8X8_USE_PINS
/*=============================================*/
/*=== 3 WIRE SOFTWARE SPI ===*/
/*
replacement for a more faster u8x8_byte_3wire_sw_spi
in general u8x8_byte_3wire_sw_spi could be a fallback:
uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
return u8x8_byte_3wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
}
*/
#ifndef __AVR_ARCH__
#define __AVR_ARCH__ 0
#endif
#if !defined(U8X8_USE_PINS)
/* no pin information (very strange), so fallback */
uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
return u8x8_byte_3wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
}
#elif __AVR_ARCH__ == 4 || __AVR_ARCH__ == 5 || __AVR_ARCH__ == 51 || __AVR_ARCH__ == 6 || __AVR_ARCH__ == 103
/* this function completely replaces u8x8_byte_4wire_sw_spi*/
extern "C" uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
uint8_t i;
uint8_t takeover_edge = u8x8_GetSPIClockPhase(u8x8);
uint16_t b;
uint8_t *data;
/* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
/* so, it should be possible to use multiple displays with different pins */
static volatile uint8_t *arduino_clock_port;
static uint8_t arduino_clock_mask;
static uint8_t arduino_clock_n_mask;
static volatile uint8_t *arduino_data_port;
static uint8_t arduino_data_mask;
static uint8_t arduino_data_n_mask;
static uint8_t last_dc;
switch(msg)
{
case U8X8_MSG_BYTE_SEND:
data = (uint8_t *)arg_ptr;
if ( takeover_edge == 0 )
{
while( arg_int > 0 )
{
b = *data;
if ( last_dc != 0 )
b |= 256;
data++;
arg_int--;
/* issue 156, check for speed */
#if F_CPU <= 17000000
if ( b == 0 )
{
*arduino_data_port &= arduino_data_n_mask;
for( i = 0; i < 9; i++ )
{
*arduino_clock_port |= arduino_clock_mask;
*arduino_clock_port &= arduino_clock_n_mask;
}
}
else
#endif
{
for( i = 0; i < 9; i++ )
{
if ( b & 256 )
*arduino_data_port |= arduino_data_mask;
else
*arduino_data_port &= arduino_data_n_mask;
*arduino_clock_port |= arduino_clock_mask;
b <<= 1;
*arduino_clock_port &= arduino_clock_n_mask;
}
}
}
}
else
{
while( arg_int > 0 )
{
b = *data;
if ( last_dc != 0 )
b |= 256;
data++;
arg_int--;
/* issue 156, check for speed */
#if F_CPU <= 17000000
if ( b == 0 )
{
*arduino_data_port &= arduino_data_n_mask;
for( i = 0; i < 9; i++ )
{
*arduino_clock_port &= arduino_clock_n_mask;
*arduino_clock_port |= arduino_clock_mask;
}
}
else
#endif
{
for( i = 0; i < 9; i++ )
{
if ( b & 256 )
*arduino_data_port |= arduino_data_mask;
else
*arduino_data_port &= arduino_data_n_mask;
*arduino_clock_port &= arduino_clock_n_mask;
b <<= 1;
*arduino_clock_port |= arduino_clock_mask;
}
}
}
}
break;
case U8X8_MSG_BYTE_INIT:
/* disable chipselect */
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
/* no wait required here */
/* for SPI: setup correct level of the clock signal */
u8x8_gpio_SetSPIClock(u8x8, u8x8_GetSPIClockPhase(u8x8));
break;
case U8X8_MSG_BYTE_SET_DC:
last_dc = arg_int;
break;
case U8X8_MSG_BYTE_START_TRANSFER:
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
/* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_CLOCK] */
arduino_clock_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK]));
arduino_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_CLOCK]);
arduino_clock_n_mask = ~arduino_clock_mask;
/* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_DATA] */
arduino_data_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA]));
arduino_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_DATA]);
arduino_data_n_mask = ~arduino_data_mask;
break;
case U8X8_MSG_BYTE_END_TRANSFER:
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
break;
default:
return 0;
}
return 1;
}
#else
/* fallback */
uint8_t u8x8_byte_arduino_3wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
return u8x8_byte_3wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
}
#endif
/*=============================================*/
/*=== 4 WIRE SOFTWARE SPI ===*/
/*
replacement for a more faster u8x8_byte_4wire_sw_spi
in general u8x8_byte_4wire_sw_spi could be a fallback:
uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
return u8x8_byte_4wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
}
*/
#ifndef __AVR_ARCH__
#define __AVR_ARCH__ 0
#endif
#if !defined(U8X8_USE_PINS)
/* no pin information (very strange), so fallback */
uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
return u8x8_byte_4wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
}
#elif __AVR_ARCH__ == 4 || __AVR_ARCH__ == 5 || __AVR_ARCH__ == 51 || __AVR_ARCH__ == 6 || __AVR_ARCH__ == 103
/* this function completely replaces u8x8_byte_4wire_sw_spi*/
extern "C" uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
uint8_t SREG_backup;
uint8_t i, b;
uint8_t *data;
uint8_t takeover_edge = u8x8_GetSPIClockPhase(u8x8);
//uint8_t not_takeover_edge = 1 - takeover_edge;
/* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
/* so, it should be possible to use multiple displays with different pins */
static volatile uint8_t *arduino_clock_port;
static uint8_t arduino_clock_mask;
static uint8_t arduino_clock_n_mask;
static volatile uint8_t *arduino_data_port;
static uint8_t arduino_data_mask;
static uint8_t arduino_data_n_mask;
switch(msg)
{
case U8X8_MSG_BYTE_SEND:
data = (uint8_t *)arg_ptr;
if ( takeover_edge == 0 )
{
while( arg_int > 0 )
{
b = *data;
data++;
arg_int--;
SREG_backup = SREG; cli();
/* issue 156, check for speed */
#if F_CPU <= 17000000
if ( b == 0 )
{
*arduino_data_port &= arduino_data_n_mask;
for( i = 0; i < 8; i++ )
{
*arduino_clock_port |= arduino_clock_mask;
*arduino_clock_port &= arduino_clock_n_mask;
}
}
else
#endif
{
for( i = 0; i < 8; i++ )
{
if ( b & 128 )
*arduino_data_port |= arduino_data_mask;
else
*arduino_data_port &= arduino_data_n_mask;
*arduino_clock_port |= arduino_clock_mask;
b <<= 1;
*arduino_clock_port &= arduino_clock_n_mask;
}
}
SREG = SREG_backup;
}
}
else
{
while( arg_int > 0 )
{
b = *data;
data++;
arg_int--;
SREG_backup = SREG; cli();
/* issue 156, check for speed */
#if F_CPU <= 17000000
if ( b == 0 )
{
*arduino_data_port &= arduino_data_n_mask;
for( i = 0; i < 8; i++ )
{
*arduino_clock_port &= arduino_clock_n_mask;
*arduino_clock_port |= arduino_clock_mask;
}
}
else
#endif
{
for( i = 0; i < 8; i++ )
{
if ( b & 128 )
*arduino_data_port |= arduino_data_mask;
else
*arduino_data_port &= arduino_data_n_mask;
*arduino_clock_port &= arduino_clock_n_mask;
b <<= 1;
*arduino_clock_port |= arduino_clock_mask;
}
}
SREG = SREG_backup;
}
}
break;
case U8X8_MSG_BYTE_INIT:
/* disable chipselect */
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
/* no wait required here */
/* for SPI: setup correct level of the clock signal */
u8x8_gpio_SetSPIClock(u8x8, u8x8_GetSPIClockPhase(u8x8));
break;
case U8X8_MSG_BYTE_SET_DC:
u8x8_gpio_SetDC(u8x8, arg_int);
break;
case U8X8_MSG_BYTE_START_TRANSFER:
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
/* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_CLOCK] */
arduino_clock_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK]));
arduino_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_CLOCK]);
arduino_clock_n_mask = ~arduino_clock_mask;
/* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_DATA] */
arduino_data_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA]));
arduino_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_DATA]);
arduino_data_n_mask = ~arduino_data_mask;
break;
case U8X8_MSG_BYTE_END_TRANSFER:
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
break;
default:
return 0;
}
return 1;
}
#elif defined(__SAM3X8E__) /* Arduino DUE */
/* this function completely replaces u8x8_byte_4wire_sw_spi*/
extern "C" uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
uint8_t i, b;
uint16_t us = ((u8x8->display_info->sck_pulse_width_ns + 999)/1000);
uint8_t *data;
uint8_t takeover_edge = u8x8_GetSPIClockPhase(u8x8);
//uint8_t not_takeover_edge = 1 - takeover_edge;
/* the following static vars are recalculated in U8X8_MSG_BYTE_START_TRANSFER */
/* so, it should be possible to use multiple displays with different pins */
/*
static volatile uint32_t *arduino_clock_port;
static uint32_t arduino_clock_mask;
static uint32_t arduino_clock_n_mask;
static volatile uint32_t *arduino_data_port;
static uint32_t arduino_data_mask;
static uint32_t arduino_data_n_mask;
*/
static WoReg *arduinoSetClockPort, *arduinoUnsetClockPort;
static uint32_t arduino_clock_mask;
static WoReg *arduinoSetDataPort, *arduinoUnsetDataPort;
static uint32_t arduino_data_mask;
switch(msg)
{
case U8X8_MSG_BYTE_SEND:
data = (uint8_t *)arg_ptr;
if ( takeover_edge == 0 )
{
while( arg_int > 0 )
{
b = *data;
data++;
arg_int--;
{
for( i = 0; i < 8; i++ )
{
/*
if ( b & 128 )
*arduino_data_port |= arduino_data_mask;
else
*arduino_data_port &= arduino_data_n_mask;
*/
if (b & 128)
*arduinoSetDataPort = arduino_data_mask;
else
*arduinoUnsetDataPort = arduino_data_mask;
//delayMicroseconds(us);
//*arduino_clock_port |= arduino_clock_mask;
*arduinoSetClockPort = arduino_clock_mask;
b <<= 1;
delayMicroseconds(us);
//*arduino_clock_port &= arduino_clock_n_mask;
*arduinoUnsetClockPort = arduino_clock_mask;
}
}
}
}
else
{
while( arg_int > 0 )
{
b = *data;
data++;
arg_int--;
{
for( i = 0; i < 8; i++ )
{
/*
if ( b & 128 )
*arduino_data_port |= arduino_data_mask;
else
*arduino_data_port &= arduino_data_n_mask;
*/
if (b & 128)
*arduinoSetDataPort = arduino_data_mask;
else
*arduinoUnsetDataPort = arduino_data_mask;
//delayMicroseconds(us);
//*arduino_clock_port &= arduino_clock_n_mask;
*arduinoUnsetClockPort = arduino_clock_mask;
b <<= 1;
delayMicroseconds(us);
//*arduino_clock_port |= arduino_clock_mask;
*arduinoSetClockPort = arduino_clock_mask;
}
}
}
}
break;
case U8X8_MSG_BYTE_INIT:
/* disable chipselect */
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
/* no wait required here */
/* for SPI: setup correct level of the clock signal */
u8x8_gpio_SetSPIClock(u8x8, u8x8_GetSPIClockPhase(u8x8));
break;
case U8X8_MSG_BYTE_SET_DC:
u8x8_gpio_SetDC(u8x8, arg_int);
break;
case U8X8_MSG_BYTE_START_TRANSFER:
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
/* there is no consistency checking for u8x8->pins[U8X8_PIN_SPI_CLOCK] */
/*
arduino_clock_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK]));
arduino_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_CLOCK]);
arduino_clock_n_mask = ~arduino_clock_mask;
arduino_data_port = portOutputRegister(digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA]));
arduino_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_DATA]);
arduino_data_n_mask = ~arduino_data_mask;
*/
arduinoSetClockPort = &digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK])->PIO_SODR;
arduinoUnsetClockPort = &digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_CLOCK])->PIO_CODR;
arduino_clock_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_CLOCK]);
arduinoSetDataPort = &digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA])->PIO_SODR;
arduinoUnsetDataPort = &digitalPinToPort(u8x8->pins[U8X8_PIN_SPI_DATA])->PIO_CODR;
arduino_data_mask = digitalPinToBitMask(u8x8->pins[U8X8_PIN_SPI_DATA]);
break;
case U8X8_MSG_BYTE_END_TRANSFER:
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
break;
default:
return 0;
}
return 1;
}
#else
/* fallback */
uint8_t u8x8_byte_arduino_4wire_sw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
return u8x8_byte_4wire_sw_spi(u8x8, msg,arg_int, arg_ptr);
}
#endif
/*=============================================*/
/*=== 3 WIRE HARDWARE SPI with 8 bit HW SPI Subsystem ===*/
/*
references:
https://github.com/olikraus/ucglib/blob/master/cppsrc/Ucglib.cpp#L581
https://github.com/olikraus/u8g2/issues/1041
*/
static uint8_t arduino_hw_spi_3w_buffer[9];
static uint8_t arduino_hw_spi_3w_bytepos;
static uint16_t arduino_hw_spi_3w_dc; // 0 = dc==0, 256 = dc==1
static void arduino_hw_spi_3w_init()
{
memset(arduino_hw_spi_3w_buffer, 0, 9);
arduino_hw_spi_3w_bytepos = 0;
}
static void arduino_hw_spi_3w_flush(void)
{
#ifdef U8X8_HAVE_HW_SPI
uint8_t i;
for(i = 0; i <= arduino_hw_spi_3w_bytepos; i++)
{
SPI.transfer(arduino_hw_spi_3w_buffer[i]);
}
#endif
}
static void arduino_hw_spi_3w_sendbyte(uint8_t data)
{
static union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } data16; // well well, not legal ISO 9899 code
data16.val = (arduino_hw_spi_3w_dc + data) << (7 - arduino_hw_spi_3w_bytepos);
#ifdef __BYTE_ORDER__
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.msb;
++arduino_hw_spi_3w_bytepos;
arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.lsb;
#else
arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.lsb;
++arduino_hw_spi_3w_bytepos;
arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.msb;
#endif
#else // __BYTE_ORDER__ not defined (no gcc)
// assume little endian
arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.msb;
++arduino_hw_spi_3w_bytepos;
arduino_hw_spi_3w_buffer[arduino_hw_spi_3w_bytepos] |= data16.lsb;
#endif
if (arduino_hw_spi_3w_bytepos == 8)
{
arduino_hw_spi_3w_flush();
arduino_hw_spi_3w_init();
}
}
extern "C" uint8_t u8x8_byte_arduino_3wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
#ifdef U8X8_HAVE_HW_SPI
uint8_t *data;
uint8_t internal_spi_mode;
switch(msg)
{
case U8X8_MSG_BYTE_SEND:
data = (uint8_t *)arg_ptr;
while(arg_int > 0) {
arduino_hw_spi_3w_sendbyte((uint8_t)*data);
data++;
arg_int--;
}
break;
case U8X8_MSG_BYTE_INIT:
if ( u8x8->bus_clock == 0 ) /* issue 769 */
u8x8->bus_clock = u8x8->display_info->sck_clock_hz;
/* disable chipselect */
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
#if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP32)
/* ESP32 has the following begin: SPI.begin(int8_t sck=SCK, int8_t miso=MISO, int8_t mosi=MOSI, int8_t ss=-1); */
/* not sure about ESP8266 */
/* apply bugfix from PR 2123 */
//if ( u8x8->pins[U8X8_PIN_I2C_CLOCK] != U8X8_PIN_NONE && u8x8->pins[U8X8_PIN_I2C_DATA] != U8X8_PIN_NONE )
if ( u8x8->pins[U8X8_PIN_SPI_CLOCK] != U8X8_PIN_NONE && u8x8->pins[U8X8_PIN_SPI_DATA] != U8X8_PIN_NONE )
{
/* SPI.begin(int8_t sck=SCK, int8_t miso=MISO, int8_t mosi=MOSI, int8_t ss=-1); */
/* actually MISO is not used, but what else could be used here??? */
//SPI.begin(u8x8->pins[U8X8_PIN_I2C_CLOCK], MISO, u8x8->pins[U8X8_PIN_I2C_DATA]);
SPI.begin(u8x8->pins[U8X8_PIN_SPI_CLOCK], MISO, u8x8->pins[U8X8_PIN_SPI_DATA]);
}
else
{
SPI.begin();
}
#else
SPI.begin();
#endif
break;
case U8X8_MSG_BYTE_SET_DC:
arduino_hw_spi_3w_dc = arg_int ? 256 : 0;
break;
case U8X8_MSG_BYTE_START_TRANSFER:
/* SPI mode has to be mapped to the mode of the current controller;
at least Uno, Due, 101 have different SPI_MODEx values */
internal_spi_mode = 0;
switch(u8x8->display_info->spi_mode) {
case 0: internal_spi_mode = SPI_MODE0; break;
case 1: internal_spi_mode = SPI_MODE1; break;
case 2: internal_spi_mode = SPI_MODE2; break;
case 3: internal_spi_mode = SPI_MODE3; break;
}
#if ARDUINO >= 10600
SPI.beginTransaction(
SPISettings(u8x8->bus_clock, MSBFIRST, internal_spi_mode));
#else
SPI.begin();
if (u8x8->display_info->sck_pulse_width_ns < 70)
SPI.setClockDivider(SPI_CLOCK_DIV2);
else if (u8x8->display_info->sck_pulse_width_ns < 140)
SPI.setClockDivider(SPI_CLOCK_DIV4);
else
SPI.setClockDivider(SPI_CLOCK_DIV8);
SPI.setDataMode(internal_spi_mode);
SPI.setBitOrder(MSBFIRST);
#endif
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
u8x8->gpio_and_delay_cb(
u8x8,
U8X8_MSG_DELAY_NANO,
u8x8->display_info->post_chip_enable_wait_ns,
NULL);
arduino_hw_spi_3w_init();
break;
case U8X8_MSG_BYTE_END_TRANSFER:
u8x8->gpio_and_delay_cb(
u8x8,
U8X8_MSG_DELAY_NANO,
u8x8->display_info->pre_chip_disable_wait_ns,
NULL);
if (arduino_hw_spi_3w_bytepos)
arduino_hw_spi_3w_flush();
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
#if ARDUINO >= 10600
SPI.endTransaction();
#else
SPI.end();
#endif
break;
default:
return 0;
}
#endif // U8X8_HAVE_HW_SPI
return 1;
}
/*=============================================*/
/*=== 4 WIRE HARDWARE SPI ===*/
#ifdef U8X8_USE_PINS
extern "C" uint8_t u8x8_byte_arduino_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
#ifdef U8X8_HAVE_HW_SPI
#if !defined(ESP_PLATFORM)
uint8_t *data;
#endif
uint8_t internal_spi_mode;
switch(msg)
{
case U8X8_MSG_BYTE_SEND:
#if defined(ESP_PLATFORM)
//T.M.L 2023-02-28: use the block transfer function on ESP, which does not overwrite the buffer.
SPI.writeBytes((uint8_t*)arg_ptr, arg_int);
#else
// 1.6.5 offers a block transfer, but the problem is, that the
// buffer is overwritten with the incoming data
// so it can not be used...
// SPI.transfer((uint8_t *)arg_ptr, arg_int);
data = (uint8_t *)arg_ptr;
while( arg_int > 0 )
{
SPI.transfer((uint8_t)*data);
data++;
arg_int--;
}
#endif
break;
case U8X8_MSG_BYTE_INIT:
if ( u8x8->bus_clock == 0 ) /* issue 769 */
u8x8->bus_clock = u8x8->display_info->sck_clock_hz;
/* disable chipselect */
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
/* no wait required here */
/* for SPI: setup correct level of the clock signal */
// removed, use SPI.begin() instead: pinMode(11, OUTPUT);
// removed, use SPI.begin() instead: pinMode(13, OUTPUT);
// removed, use SPI.begin() instead: digitalWrite(13, u8x8_GetSPIClockPhase(u8x8));
/* setup hardware with SPI.begin() instead of previous digitalWrite() and pinMode() calls */
/* issue #377 */
/* issue #378: removed ESP8266 support, which is implemented differently */
#if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP32)
/* ESP32 has the following begin: SPI.begin(int8_t sck=SCK, int8_t miso=MISO, int8_t mosi=MOSI, int8_t ss=-1); */
/* not sure about ESP8266 */
if ( u8x8->pins[U8X8_PIN_SPI_CLOCK] != U8X8_PIN_NONE && u8x8->pins[U8X8_PIN_SPI_DATA] != U8X8_PIN_NONE )
{
/* SPI.begin(int8_t sck=SCK, int8_t miso=MISO, int8_t mosi=MOSI, int8_t ss=-1); */
/* actually MISO is not used, but what else could be used here??? */
SPI.begin(u8x8->pins[U8X8_PIN_SPI_CLOCK], MISO, u8x8->pins[U8X8_PIN_SPI_DATA]);
}
else
{
SPI.begin();
}
#else
SPI.begin();
#endif
break;
case U8X8_MSG_BYTE_SET_DC:
u8x8_gpio_SetDC(u8x8, arg_int);
break;
case U8X8_MSG_BYTE_START_TRANSFER:
/* SPI mode has to be mapped to the mode of the current controller, at least Uno, Due, 101 have different SPI_MODEx values */
internal_spi_mode = 0;
switch(u8x8->display_info->spi_mode)
{
case 0: internal_spi_mode = SPI_MODE0; break;
case 1: internal_spi_mode = SPI_MODE1; break;
case 2: internal_spi_mode = SPI_MODE2; break;
case 3: internal_spi_mode = SPI_MODE3; break;
}
#if ARDUINO >= 10600
SPI.beginTransaction(SPISettings(u8x8->bus_clock, MSBFIRST, internal_spi_mode));
#else
SPI.begin();
if ( u8x8->display_info->sck_pulse_width_ns < 70 )
SPI.setClockDivider( SPI_CLOCK_DIV2 );
else if ( u8x8->display_info->sck_pulse_width_ns < 140 )
SPI.setClockDivider( SPI_CLOCK_DIV4 );
else
SPI.setClockDivider( SPI_CLOCK_DIV8 );
SPI.setDataMode(internal_spi_mode);
SPI.setBitOrder(MSBFIRST);
#endif
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->post_chip_enable_wait_ns, NULL);
break;
case U8X8_MSG_BYTE_END_TRANSFER:
u8x8->gpio_and_delay_cb(u8x8, U8X8_MSG_DELAY_NANO, u8x8->display_info->pre_chip_disable_wait_ns, NULL);
u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
#if ARDUINO >= 10600
SPI.endTransaction();
#else
SPI.end();
#endif
break;
default: