-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSP.c
2130 lines (1954 loc) · 91.8 KB
/
BSP.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
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
// BSP.c
// Runs on TM4C123 with an Educational BoosterPack MKII (BOOSTXL-EDUMKII)
// This file contains the software interface to the MKII BoosterPack.
// This board support package (BSP) is an abstraction layer,
// forming a bridge between the low-level hardware and the high-level software.
// Remember to remove R9 and R10 to use the LCD since R10
// connects PD1 (accelerometer Y) to PB7 (LCD SPI data).
// Daniel and Jonathan Valvano
// June 8, 2016
/* This example accompanies the books
"Embedded Systems: Introduction to ARM Cortex M Microcontrollers",
ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2016
"Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers",
ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2016
"Embedded Systems: Real-Time Operating Systems for ARM Cortex-M Microcontrollers",
ISBN: 978-1466468863, , Jonathan Valvano, copyright (c) 2016
Copyright 2016 by Jonathan W. Valvano, valvano@mail.utexas.edu
You may use, edit, run or distribute this file
as long as the above copyright notice remains
THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
For more information about my classes, my research, and my books, see
http://users.ece.utexas.edu/~valvano/
*/
// J1 J3 J4 J2
// [ 1] [21] [40] [20]
// [ 2] [22] [39] [19]
// [ 3] [23] [38] [18]
// [ 4] [24] [37] [17]
// [ 5] [25] [36] [16]
// [ 6] [26] [35] [15]
// [ 7] [27] [34] [14]
// [ 8] [28] [33] [13]
// [ 9] [29] [32] [12]
// [10] [30] [31] [11]
// Connected pins in physical order
// J1.1 +3.3V (power)
// J1.2 joystick horizontal (X) (analog) {TM4C123 PB5/AIN11, MSP432 P6.0}
// J1.3 UART from Bluetooth to LaunchPad (UART) {TM4C123 PB0, MSP432 P3.2}
// J1.4 UART from LaunchPad to Bluetooth (UART) {TM4C123 PB1, MSP432 P3.3}
// J1.5 joystick Select button (digital) {TM4C123 PE4, MSP432 P4.1}
// J1.6 microphone (analog) {TM4C123 PE5/AIN8, MSP432 P4.3}
// J1.7 LCD SPI clock (SPI) {TM4C123 PB4, MSP432 P1.5}
// J1.8 ambient light (OPT3001) interrupt (digital) {TM4C123 PA5, MSP432 P4.6}
// J1.9 ambient light (OPT3001) and temperature sensor (TMP006) I2C SCL (I2C) {TM4C123 PA6, MSP432 P6.5}
// J1.10 ambient light (OPT3001) and temperature sensor (TMP006) I2C SDA (I2C) {TM4C123 PA7, MSP432 P6.4}
//--------------------------------------------------
// J2.11 temperature sensor (TMP006) interrupt (digital) {TM4C123 PA2, MSP432 P3.6}
// J2.12 nothing {TM4C123 PA3, MSP432 P5.2}
// J2.13 LCD SPI CS (SPI) {TM4C123 PA4, MSP432 P5.0}
// J2.14 nothing {TM4C123 PB6, MSP432 P1.7}
// J2.15 LCD SPI data (SPI) {TM4C123 PB7, MSP432 P1.6}
// J2.16 nothing (reset)
// J2.17 LCD !RST (digital) {TM4C123 PF0, MSP432 P5.7}
// J2.18 Profile 4 {TM4C123 PE0, MSP432 P3.0}
// J2.19 servo PWM {TM4C123 PB2, MSP432 P2.5}
// J2.20 GND (ground)
//--------------------------------------------------
// J3.21 +5V (power)
// J3.22 GND (ground)
// J3.23 accelerometer X (analog) {TM4C123 PD0/AIN7, MSP432 P6.1}
// J3.24 accelerometer Y (analog) {TM4C123 PD1/AIN6, MSP432 P4.0}
// J3.25 accelerometer Z (analog) {TM4C123 PD2/AIN5, MSP432 P4.2}
// J3.26 joystick vertical (Y) (analog) {TM4C123 PD3/AIN4, MSP432 P4.4}
// J3.27 Profile 0 {TM4C123 PE1, MSP432 P4.5}
// J3.28 Profile 1 {TM4C123 PE2, MSP432 P4.7}
// J3.29 Profile 2 {TM4C123 PE3, MSP432 P5.4}
// J3.30 Profile 3 {TM4C123 PF1, MSP432 P5.5}
//--------------------------------------------------
// J4.31 LCD RS (digital) {TM4C123 PF4, MSP432 P3.7}
// J4.32 user Button2 (bottom) (digital) {TM4C123 PD7, MSP432 P3.5}
// J4.33 user Button1 (top) (digital) {TM4C123 PD6, MSP432 P5,1}
// J4.34 Profile 6/gator hole switch {TM4C123 PC7, MSP432 P2.3}
// J4.35 nothing {TM4C123 PC6, MSP432 P6.7}
// J4.36 Profile 5 {TM4C123 PC5, MSP432 P6.6}
// J4.37 RGB LED blue (PWM) {TM4C123 PC4, MSP432 P5.6}
// J4.38 RGB LED green (PWM) {TM4C123 PB3, MSP432 P2.4}
// J4.39 RGB LED red (jumper up) or LCD backlight (jumper down) (PWM) {TM4C123 PF3, MSP432 P2.6}
// J4.40 buzzer (PWM) {TM4C123 PF2, MSP432 P2.7}
//--------------------------------------------------
// Connected pins in logic order
// power and reset
// J1.1 +3.3V (power)
// J3.21 +5V (power)
// J3.22 GND (ground)
// J2.20 GND (ground)
// J2.16 nothing (reset)
//--------------------------------------------------
// LCD graphics
// J1.7 LCD SPI clock (SPI) {TM4C123 PB4, MSP432 P1.5}
// J2.13 LCD SPI CS (SPI) {TM4C123 PA4, MSP432 P5.0}
// J2.15 LCD SPI data (SPI) {TM4C123 PB7, MSP432 P1.6}
// J2.17 LCD !RST (digital) {TM4C123 PF0, MSP432 P5.7}
// J4.31 LCD RS (digital) {TM4C123 PF4, MSP432 P3.7}
//--------------------------------------------------
// 3-color LED
// J4.37 RGB LED blue (PWM) {TM4C123 PC4, MSP432 P5.6}
// J4.38 RGB LED green (PWM) {TM4C123 PB3, MSP432 P2.4}
// J4.39 RGB LED red (jumper up) or LCD backlight (jumper down) (PWM) {TM4C123 PF3, MSP432 P2.6}
//--------------------------------------------------
// user buttons
// J4.32 user Button2 (bottom) (digital) {TM4C123 PD7, MSP432 P3.5}
// J4.33 user Button1 (top) (digital) {TM4C123 PD6, MSP432 P5.1}
//--------------------------------------------------
// buzzer output
// J4.40 buzzer (PWM) {TM4C123 PF2, MSP432 P2.7}
//--------------------------------------------------
// Joystick
// J1.5 joystick Select button (digital) {TM4C123 PE4, MSP432 P4.1}
// J1.2 joystick horizontal (X) (analog) {TM4C123 PB5/AIN11, MSP432 P6.0}
// J3.26 joystick vertical (Y) (analog) {TM4C123 PD3/AIN4, MSP432 P4.4}
//--------------------------------------------------
// accelerometer
// J3.23 accelerometer X (analog) {TM4C123 PD0/AIN7, MSP432 P6.1}
// J3.24 accelerometer Y (analog) {TM4C123 PD1/AIN6, MSP432 P4.0}
// J3.25 accelerometer Z (analog) {TM4C123 PD2/AIN5, MSP432 P4.2}
//--------------------------------------------------
// microphone
// J1.6 microphone (analog) {TM4C123 PE5/AIN8, MSP432 P4.3}
//--------------------------------------------------
// light and temperature sensors (I2C)
// J1.8 ambient light (OPT3001) interrupt (digital) {TM4C123 PA5, MSP432 P4.6}
// J1.9 ambient light (OPT3001) and temperature sensor (TMP006) I2C SCL (I2C) {TM4C123 PA6, MSP432 P6.5}
// J1.10 ambient light (OPT3001) and temperature sensor (TMP006) I2C SDA (I2C) {TM4C123 PA7, MSP432 P6.4}
// J2.11 temperature sensor (TMP006) interrupt (digital) {TM4C123 PA2, MSP432 P3.6}
//--------------------------------------------------
// Bluetooth booster
// J1.3 UART from Bluetooth to LaunchPad (UART) {TM4C123 PB0, MSP432 P3.2}
// J1.4 UART from LaunchPad to Bluetooth (UART) {TM4C123 PB1, MSP432 P3.3}
//--------------------------------------------------
// profile pins
// J3.27 Profile 0 {TM4C123 PE1, MSP432 P4.5}
// J3.28 Profile 1 {TM4C123 PE2, MSP432 P4.7}
// J3.29 Profile 2 {TM4C123 PE3, MSP432 P5.4}
// J3.30 Profile 3 {TM4C123 PF1, MSP432 P5.5}
// J2.18 Profile 4 {TM4C123 PE0, MSP432 P3.0}
// J4.36 Profile 5 {TM4C123 PC5, MSP432 P6.6}
// J4.34 Profile 6 {TM4C123 PC7, MSP432 P2.3}
//--------------------------------------------------
// unconnected pins
// J2.12 nothing {TM4C123 PA3, MSP432 P5.2}
// J2.14 nothing {TM4C123 PB6, MSP432 P1.7}
// J2.19 servo PWM {TM4C123 PB2, MSP432 P2.5}
// J4.35 nothing {TM4C123 PC6, MSP432 P6.7}
#include <stdint.h>
#include "BSP.h"
#include "tm4c123gh6pm.h"
void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void); // Enable interrupts
long StartCritical (void); // previous I bit, disable interrupts
void EndCritical(long sr); // restore I bit to previous value
void WaitForInterrupt(void); // low power mode
static uint32_t ClockFrequency = 16000000; // cycles/second
// ------------BSP_Button1_Init------------
// Initialize a GPIO pin for input, which corresponds
// with BoosterPack pin J4.33.
// Input: none
// Output: none
// ------------BSP_Button1_Input------------
// Read and return the immediate status of Button1.
// Button de-bouncing is not considered.
// Input: none
// Output: non-zero if Button1 unpressed
// zero if Button1 pressed
// Assumes: BSP_Button1_Init() has been called
#define BUTTON1 (*((volatile uint32_t *)0x40007100)) /* PD6 */
// ------------BSP_Button2_Init------------
// Initialize a GPIO pin for input, which corresponds
// with BoosterPack pin J4.32.
// Input: none
// Output: none
// ------------BSP_Button2_Input------------
// Read and return the immediate status of Button2.
// Button de-bouncing is not considered.
// Input: none
// Output: non-zero if Button2 unpressed
// zero if Button2 pressed
// Assumes: BSP_Button2_Init() has been called
#define BUTTON2 (*((volatile uint32_t *)0x40007200)) /* PD7 */
// There are six analog inputs on the Educational BoosterPack MKII:
// microphone (J1.6/PE5/AIN8)
// joystick X (J1.2/PB5/AIN11) and Y (J3.26/PD3/AIN4)
// accelerometer X (J3.23/PD0/AIN7), Y (J3.24/PD1/AIN6), and Z (J3.25/PD2/AIN5)
// All six initialization functions can use this general ADC
// initialization. The joystick uses sample sequencer 1,
// the accelerometer sample sequencer 2, and the microphone
// uses sample sequencer 3.
void static adcinit(void){
SYSCTL_RCGCADC_R |= 0x00000001; // 1) activate ADC0
while((SYSCTL_PRADC_R&0x01) == 0){};// 2) allow time for clock to stabilize
// 3-7) GPIO initialization in more specific functions
ADC0_PC_R &= ~0xF; // 8) clear max sample rate field
ADC0_PC_R |= 0x1; // configure for 125K samples/sec
ADC0_SSPRI_R = 0x3210; // 9) Sequencer 3 is lowest priority
// 10-15) sample sequencer initialization in more specific functions
}
// ------------BSP_Joystick_Init------------
// Initialize a GPIO pin for input, which corresponds
// with BoosterPack pin J1.5 (Select button).
// Initialize two ADC pins, which correspond with
// BoosterPack pins J1.2 (X) and J3.26 (Y).
// Input: none
// Output: none
// ------------BSP_Joystick_Input------------
// Read and return the immediate status of the
// joystick. Button de-bouncing for the Select
// button is not considered. The joystick X- and
// Y-positions are returned as 10-bit numbers,
// even if the ADC on the LaunchPad is more precise.
// Input: x is pointer to store X-position (0 to 1023)
// y is pointer to store Y-position (0 to 1023)
// select is pointer to store Select status (0 if pressed)
// Output: none
// Assumes: BSP_Joystick_Init() has been called
#define SELECT (*((volatile uint32_t *)0x40024040)) /* PE4 */
// ------------BSP_RGB_Init------------
// Initialize the GPIO and PWM or timer modules which
// correspond with BoosterPack pins J4.39 (red),
// J4.38 (green), and J4.37 (blue). The frequency
// must be fast enough to not appear to flicker, and
// the duty cycle is represented as a 10-bit number.
// 1023 is fully (or nearly fully) on.
// 0 is fully (or nearly fully) off.
// Input: red is 10-bit duty cycle for red
// green is 10-bit duty cycle for green
// blue is 10-bit duty cycle for blue
// Output: none
static uint16_t PWMCycles; // number of PWM cycles per period
// ------------BSP_RGB_Set------------
// Set new duty cycles for the RGB LEDs.
// 1023 is fully (or nearly fully) on.
// 0 is fully (or nearly fully) off.
// Input: red is 10-bit duty cycle for red
// green is 10-bit duty cycle for green
// blue is 10-bit duty cycle for blue
// Output: none
// Assumes: BSP_RGB_Init() has been called
// ------------BSP_RGB_D_Init------------
// Initialize the GPIO pins for output which
// correspond with BoosterPack pins J4.39 (red),
// J4.38 (green), and J4.37 (blue). Instead of using
// PWM or timer modules, this configuration uses just
// digital fully on or fully off.
// non-zero is fully on.
// 0 is fully off.
// Input: red is initial status for red
// green is initial status for green
// blue is initial status for blue
// Output: none
#define RED (*((volatile uint32_t *)0x40025020)) /* PF3 */
#define GREEN (*((volatile uint32_t *)0x40005020)) /* PB3 */
#define BLUE (*((volatile uint32_t *)0x40006040)) /* PC4 */
void BSP_RGB_D_Init(int red, int green, int blue){
SYSCTL_RCGCGPIO_R |= 0x00000026; // 1) activate clock for Ports F, C, and B
while((SYSCTL_PRGPIO_R&0x26) != 0x26){};// allow time for clocks to stabilize
// 2) no need to unlock PF3, PC4, or PB3
GPIO_PORTF_AMSEL_R &= ~0x08; // 3a) disable analog on PF3
GPIO_PORTC_AMSEL_R &= ~0x10; // 3b) disable analog on PC4
GPIO_PORTB_AMSEL_R &= ~0x08; // 3c) disable analog on PB3
// 4a) configure PF3 as GPIO
GPIO_PORTF_PCTL_R = (GPIO_PORTF_PCTL_R&0xFFFF0FFF)+0x00000000;
// 4b) configure PC4 as GPIO
GPIO_PORTC_PCTL_R = (GPIO_PORTC_PCTL_R&0xFFF0FFFF)+0x00000000;
// 4c) configure PB3 as GPIO
GPIO_PORTB_PCTL_R = (GPIO_PORTB_PCTL_R&0xFFFF0FFF)+0x00000000;
GPIO_PORTF_DIR_R |= 0x08; // 5a) make PF3 output
GPIO_PORTC_DIR_R |= 0x10; // 5b) make PC4 output
GPIO_PORTB_DIR_R |= 0x08; // 5c) make PB3 output
GPIO_PORTF_AFSEL_R &= ~0x08; // 6a) disable alt funct on PF3
GPIO_PORTC_AFSEL_R &= ~0x10; // 6b) disable alt funct on PC4
GPIO_PORTB_AFSEL_R &= ~0x08; // 6c) disable alt funct on PB3
GPIO_PORTF_PUR_R &= ~0x08; // disable pull-up on PF3
GPIO_PORTC_PUR_R &= ~0x10; // disable pull-up on PC4
GPIO_PORTB_PUR_R &= ~0x08; // disable pull-up on PB3
GPIO_PORTF_DEN_R |= 0x08; // 7a) enable digital I/O on PF3
GPIO_PORTC_DEN_R |= 0x10; // 7b) enable digital I/O on PC4
GPIO_PORTB_DEN_R |= 0x08; // 7c) enable digital I/O on PB3
BSP_RGB_D_Set(red, green, blue);
}
// ------------BSP_RGB_D_Set------------
// Set new statuses for the RGB LEDs.
// non-zero is fully on.
// 0 is fully off.
// Input: red is status for red
// green is status for green
// blue is status for blue
// Output: none
// Assumes: BSP_RGB_D_Init() has been called
void BSP_RGB_D_Set(int red, int green, int blue){
if(red){
RED = 0x08;
} else{
RED = 0x00;
}
if(green){
GREEN = 0x08;
} else{
GREEN = 0x00;
}
if(blue){
BLUE = 0x10;
} else{
BLUE = 0x00;
}
}
// ------------BSP_RGB_D_Toggle------------
// Toggle the statuses of the RGB LEDs.
// non-zero is toggle.
// 0 is do not toggle.
// Input: red is toggle for red
// green is toggle for green
// blue is toggle for blue
// Output: none
// Assumes: BSP_RGB_D_Init() has been called
void BSP_RGB_D_Toggle(int red, int green, int blue){
if(red){
RED = RED^0x08;
}
if(green){
GREEN = GREEN^0x08;
}
if(blue){
BLUE = BLUE^0x10;
}
}
// ------------BSP_Buzzer_Init------------
// Initialize the GPIO and PWM or timer modules which
// correspond with BoosterPack pin J4.40. The
// frequency is 2048 Hz, and the duty cycle is
// represented as a 10-bit number.
// Input: duty is 10-bit duty cycle for the buzzer
// Output: none
void BSP_Buzzer_Init(uint16_t duty){
if(duty > 1023){
return; // invalid input
}
// ***************** Timer1A initialization *****************
SYSCTL_RCGCTIMER_R |= 0x02; // activate clock for Timer1
SYSCTL_RCGCGPIO_R |= 0x0020; // activate clock for Port F
while((SYSCTL_PRGPIO_R&0x20) == 0){};// allow time for clock to stabilize
GPIO_PORTF_AFSEL_R |= 0x04; // enable alt funct on PF2
GPIO_PORTF_DEN_R |= 0x04; // enable digital I/O on PF2
// configure PF2 as T1CCP0
GPIO_PORTF_PCTL_R = (GPIO_PORTF_PCTL_R&0xFFFFF0FF)+0x00000700;
GPIO_PORTF_AMSEL_R &= ~0x04; // disable analog functionality on PF2
while((SYSCTL_PRTIMER_R&0x02) == 0){};// allow time for clock to stabilize
TIMER1_CTL_R &= ~TIMER_CTL_TAEN; // disable Timer1A during setup
TIMER1_CFG_R = TIMER_CFG_16_BIT; // configure for 16-bit timer mode
// configure for alternate (PWM) mode
TIMER1_TAMR_R = (TIMER_TAMR_TAAMS|TIMER_TAMR_TAMR_PERIOD);
PWMCycles = ClockFrequency/2048;
TIMER1_TAILR_R = PWMCycles - 1; // defines when output signal is set
TIMER1_TAMATCHR_R = (duty*PWMCycles)>>10;// defines when output signal is cleared
// enable Timer1A 16-b, PWM, inverted to match comments
TIMER1_CTL_R |= (TIMER_CTL_TAPWML|TIMER_CTL_TAEN);
}
// ------------BSP_Buzzer_Set------------
// Set new duty cycle for the buzzer.
// 512 is typical for sound (50% duty cycle).
// 0 is typical for silent (0% duty cycle).
// Input: duty is 10-bit duty cycle for the buzzer
// Output: none
// Assumes: BSP_Buzzer_Init() has been called
void BSP_Buzzer_Set(uint16_t duty){
if(duty > 1023){
return; // invalid input
}
TIMER1_TAMATCHR_R = (duty*PWMCycles)>>10;// defines when output signal is cleared
}
// ------------BSP_Accelerometer_Init------------
// Initialize three ADC pins, which correspond with
// BoosterPack pins J3.23 (X), J3.24 (Y), and
// J3.25 (Y).
// Input: none
// Output: none
void BSP_Accelerometer_Init(void){
adcinit();
SYSCTL_RCGCGPIO_R |= 0x00000008; // 1) activate clock for Port D
while((SYSCTL_PRGPIO_R&0x08) == 0){};// allow time for clock to stabilize
// 2) no need to unlock PD2-0
GPIO_PORTD_AMSEL_R |= 0x07; // 3) enable analog on PD2-0
// 4) configure PD2-0 as ?? (skip this line because PCTL is for digital only)
GPIO_PORTD_DIR_R &= ~0x07; // 5) make PD2-0 input
GPIO_PORTD_AFSEL_R |= 0x07; // 6) enable alt funct on PD2-0
GPIO_PORTD_DEN_R &= ~0x07; // 7) enable analog functionality on PD2-0
adcinit(); // 8-9) general ADC initialization
ADC0_ACTSS_R &= ~0x0004; // 10) disable sample sequencer 2
ADC0_EMUX_R &= ~0x0F00; // 11) seq2 is software trigger
ADC0_SSMUX2_R = 0x0567; // 12) set channels for SS2
ADC0_SSCTL2_R = 0x0600; // 13) no D0 END0 IE0 TS0 D1 END1 IE1 TS1 D2 TS2, yes IE2 END2
ADC0_IM_R &= ~0x0004; // 14) disable SS2 interrupts
ADC0_ACTSS_R |= 0x0004; // 15) enable sample sequencer 2
}
// ------------BSP_Accelerometer_Input------------
// Read and return the immediate status of the
// accelerometer. The accelerometer X-, Y-, and
// Z-measurements are returned as 10-bit numbers,
// even if the ADC on the LaunchPad is more precise.
// Input: x is pointer to store X-measurement (0 to 1023)
// y is pointer to store Y-measurement (0 to 1023)
// z is pointer to store Z-measurement (0 to 1023)
// Output: none
// Assumes: BSP_Accelerometer_Init() has been called
void BSP_Accelerometer_Input(uint16_t *x, uint16_t *y, uint16_t *z){
ADC0_PSSI_R = 0x0004; // 1) initiate SS2
while((ADC0_RIS_R&0x04)==0){}; // 2) wait for conversion done
*x = ADC0_SSFIFO2_R>>2; // 3a) read first result
*y = ADC0_SSFIFO2_R>>2; // 3b) read second result
*z = ADC0_SSFIFO2_R>>2; // 3c) read third result
ADC0_ISC_R = 0x0004; // 4) acknowledge completion
}
// ------------BSP_Microphone_Init------------
// Initialize one ADC pin, which corresponds with
// BoosterPack pin J1.6.
// Input: none
// Output: none
void BSP_Microphone_Init(void){
adcinit();
SYSCTL_RCGCGPIO_R |= 0x00000010; // 1) activate clock for Port E
while((SYSCTL_PRGPIO_R&0x10) == 0){};// allow time for clock to stabilize
// 2) no need to unlock PE5
GPIO_PORTE_AMSEL_R |= 0x20; // 3) enable analog on PE5
// 4) configure PE5 as ?? (skip this line because PCTL is for digital only)
GPIO_PORTE_DIR_R &= ~0x20; // 5) make PE5 input
GPIO_PORTE_AFSEL_R |= 0x20; // 6) enable alt funct on PE5
GPIO_PORTE_DEN_R &= ~0x20; // 7) enable analog functionality on PE5
adcinit(); // 8-9) general ADC initialization
ADC0_ACTSS_R &= ~0x0008; // 10) disable sample sequencer 3
ADC0_EMUX_R &= ~0xF000; // 11) seq3 is software trigger
ADC0_SSMUX3_R = 0x0008; // 12) set channels for SS3
ADC0_SSCTL3_R = 0x0006; // 13) no D0 TS0, yes IE0 END0
ADC0_IM_R &= ~0x0008; // 14) disable SS3 interrupts
ADC0_ACTSS_R |= 0x0008; // 15) enable sample sequencer 3
}
// ------------BSP_Microphone_Input------------
// Read and return the immediate status of the
// microphone. The sound measurement is returned
// as a 10-bit number, even if the ADC on the
// LaunchPad is more precise.
// Input: mic is pointer to store sound measurement (0 to 1023)
// Output: none
// Assumes: BSP_Microphone_Init() has been called
void BSP_Microphone_Input(uint16_t *mic){
ADC0_PSSI_R = 0x0008; // 1) initiate SS3
while((ADC0_RIS_R&0x08)==0){}; // 2) wait for conversion done
*mic = ADC0_SSFIFO3_R>>2; // 3) read result
ADC0_ISC_R = 0x0008; // 4) acknowledge completion
}
/* ********************** */
/* LCD Section */
/* ********************** */
// This section is based on ST7735.c, which itself is based
// on example code originally from Adafruit. Some sections
// such as the font table and initialization functions were
// copied verbatim from Adafruit's example and are subject
// to the following disclosure.
/***************************************************
This is a library for the Adafruit 1.8" SPI display.
This library works with the Adafruit 1.8" TFT Breakout w/SD card
----> http://www.adafruit.com/products/358
as well as Adafruit raw 1.8" TFT displayun
----> http://www.adafruit.com/products/618
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
// some flags for ST7735_InitR()
enum initRFlags{
none,
INITR_GREENTAB,
INITR_REDTAB,
INITR_BLACKTAB
};
#define ST7735_TFTWIDTH 128
#define ST7735_TFTHEIGHT 128
// Color definitions
#define ST7735_BLACK 0x0000
#define ST7735_BLUE 0x001F
#define ST7735_RED 0xF800
#define ST7735_GREEN 0x07E0
#define ST7735_CYAN 0x07FF
#define ST7735_MAGENTA 0xF81F
#define ST7735_YELLOW 0xFFE0
#define ST7735_WHITE 0xFFFF
// 12 rows (0 to 11) and 21 characters (0 to 20)
// Requires (11 + size*size*6*8) bytes of transmission for each character
#define ST7735_NOP 0x00
#define ST7735_SWRESET 0x01
#define ST7735_RDDID 0x04
#define ST7735_RDDST 0x09
#define ST7735_SLPIN 0x10
#define ST7735_SLPOUT 0x11
#define ST7735_PTLON 0x12
#define ST7735_NORON 0x13
#define ST7735_INVOFF 0x20
#define ST7735_INVON 0x21
#define ST7735_DISPOFF 0x28
#define ST7735_DISPON 0x29
#define ST7735_CASET 0x2A
#define ST7735_RASET 0x2B
#define ST7735_RAMWR 0x2C
#define ST7735_RAMRD 0x2E
#define ST7735_PTLAR 0x30
#define ST7735_COLMOD 0x3A
#define ST7735_MADCTL 0x36
#define ST7735_FRMCTR1 0xB1
#define ST7735_FRMCTR2 0xB2
#define ST7735_FRMCTR3 0xB3
#define ST7735_INVCTR 0xB4
#define ST7735_DISSET5 0xB6
#define ST7735_PWCTR1 0xC0
#define ST7735_PWCTR2 0xC1
#define ST7735_PWCTR3 0xC2
#define ST7735_PWCTR4 0xC3
#define ST7735_PWCTR5 0xC4
#define ST7735_VMCTR1 0xC5
#define ST7735_RDID1 0xDA
#define ST7735_RDID2 0xDB
#define ST7735_RDID3 0xDC
#define ST7735_RDID4 0xDD
#define ST7735_PWCTR6 0xFC
#define ST7735_GMCTRP1 0xE0
#define ST7735_GMCTRN1 0xE1
#define TFT_CS (*((volatile uint32_t *)0x40004040)) /* PA4 */
#define TFT_CS_LOW 0x00
#define TFT_CS_HIGH 0x10
#define DC (*((volatile uint32_t *)0x40025040)) /* PF4 */
#define DC_COMMAND 0x00
#define DC_DATA 0x10
#define RESET (*((volatile uint32_t *)0x40025004)) /* PF0 */
#define RESET_LOW 0x00
#define RESET_HIGH 0x01
// standard ascii 5x7 font
// originally from glcdfont.c from Adafruit project
static const uint8_t Font[] = {
0x00, 0x00, 0x00, 0x00, 0x00,
0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
0x18, 0x3C, 0x7E, 0x3C, 0x18,
0x1C, 0x57, 0x7D, 0x57, 0x1C,
0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
0x00, 0x18, 0x3C, 0x18, 0x00,
0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
0x00, 0x18, 0x24, 0x18, 0x00,
0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
0x30, 0x48, 0x3A, 0x06, 0x0E,
0x26, 0x29, 0x79, 0x29, 0x26,
0x40, 0x7F, 0x05, 0x05, 0x07,
0x40, 0x7F, 0x05, 0x25, 0x3F,
0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
0x7F, 0x3E, 0x1C, 0x1C, 0x08,
0x08, 0x1C, 0x1C, 0x3E, 0x7F,
0x14, 0x22, 0x7F, 0x22, 0x14,
0x5F, 0x5F, 0x00, 0x5F, 0x5F,
0x06, 0x09, 0x7F, 0x01, 0x7F,
0x00, 0x66, 0x89, 0x95, 0x6A,
0x60, 0x60, 0x60, 0x60, 0x60,
0x94, 0xA2, 0xFF, 0xA2, 0x94,
0x08, 0x04, 0x7E, 0x04, 0x08,
0x10, 0x20, 0x7E, 0x20, 0x10,
0x08, 0x08, 0x2A, 0x1C, 0x08,
0x08, 0x1C, 0x2A, 0x08, 0x08,
0x1E, 0x10, 0x10, 0x10, 0x10,
0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
0x30, 0x38, 0x3E, 0x38, 0x30,
0x06, 0x0E, 0x3E, 0x0E, 0x06,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x5F, 0x00, 0x00,
0x00, 0x07, 0x00, 0x07, 0x00,
0x14, 0x7F, 0x14, 0x7F, 0x14,
0x24, 0x2A, 0x7F, 0x2A, 0x12,
0x23, 0x13, 0x08, 0x64, 0x62,
0x36, 0x49, 0x56, 0x20, 0x50,
0x00, 0x08, 0x07, 0x03, 0x00,
0x00, 0x1C, 0x22, 0x41, 0x00,
0x00, 0x41, 0x22, 0x1C, 0x00,
0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
0x08, 0x08, 0x3E, 0x08, 0x08,
0x00, 0x80, 0x70, 0x30, 0x00,
0x08, 0x08, 0x08, 0x08, 0x08,
0x00, 0x00, 0x60, 0x60, 0x00,
0x20, 0x10, 0x08, 0x04, 0x02,
0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
0x00, 0x42, 0x7F, 0x40, 0x00, // 1
0x72, 0x49, 0x49, 0x49, 0x46, // 2
0x21, 0x41, 0x49, 0x4D, 0x33, // 3
0x18, 0x14, 0x12, 0x7F, 0x10, // 4
0x27, 0x45, 0x45, 0x45, 0x39, // 5
0x3C, 0x4A, 0x49, 0x49, 0x31, // 6
0x41, 0x21, 0x11, 0x09, 0x07, // 7
0x36, 0x49, 0x49, 0x49, 0x36, // 8
0x46, 0x49, 0x49, 0x29, 0x1E, // 9
0x00, 0x00, 0x14, 0x00, 0x00,
0x00, 0x40, 0x34, 0x00, 0x00,
0x00, 0x08, 0x14, 0x22, 0x41,
0x14, 0x14, 0x14, 0x14, 0x14,
0x00, 0x41, 0x22, 0x14, 0x08,
0x02, 0x01, 0x59, 0x09, 0x06,
0x3E, 0x41, 0x5D, 0x59, 0x4E,
0x7C, 0x12, 0x11, 0x12, 0x7C, // A
0x7F, 0x49, 0x49, 0x49, 0x36, // B
0x3E, 0x41, 0x41, 0x41, 0x22, // C
0x7F, 0x41, 0x41, 0x41, 0x3E, // D
0x7F, 0x49, 0x49, 0x49, 0x41, // E
0x7F, 0x09, 0x09, 0x09, 0x01, // F
0x3E, 0x41, 0x41, 0x51, 0x73, // G
0x7F, 0x08, 0x08, 0x08, 0x7F, // H
0x00, 0x41, 0x7F, 0x41, 0x00, // I
0x20, 0x40, 0x41, 0x3F, 0x01, // J
0x7F, 0x08, 0x14, 0x22, 0x41, // K
0x7F, 0x40, 0x40, 0x40, 0x40, // L
0x7F, 0x02, 0x1C, 0x02, 0x7F, // M
0x7F, 0x04, 0x08, 0x10, 0x7F, // N
0x3E, 0x41, 0x41, 0x41, 0x3E, // O
0x7F, 0x09, 0x09, 0x09, 0x06, // P
0x3E, 0x41, 0x51, 0x21, 0x5E, // Q
0x7F, 0x09, 0x19, 0x29, 0x46, // R
0x26, 0x49, 0x49, 0x49, 0x32, // S
0x03, 0x01, 0x7F, 0x01, 0x03, // T
0x3F, 0x40, 0x40, 0x40, 0x3F, // U
0x1F, 0x20, 0x40, 0x20, 0x1F, // V
0x3F, 0x40, 0x38, 0x40, 0x3F, // W
0x63, 0x14, 0x08, 0x14, 0x63, // X
0x03, 0x04, 0x78, 0x04, 0x03, // Y
0x61, 0x59, 0x49, 0x4D, 0x43, // Z
0x00, 0x7F, 0x41, 0x41, 0x41,
0x02, 0x04, 0x08, 0x10, 0x20,
0x00, 0x41, 0x41, 0x41, 0x7F,
0x04, 0x02, 0x01, 0x02, 0x04,
0x40, 0x40, 0x40, 0x40, 0x40,
0x00, 0x03, 0x07, 0x08, 0x00,
0x20, 0x54, 0x54, 0x78, 0x40, // a
0x7F, 0x28, 0x44, 0x44, 0x38, // b
0x38, 0x44, 0x44, 0x44, 0x28, // c
0x38, 0x44, 0x44, 0x28, 0x7F, // d
0x38, 0x54, 0x54, 0x54, 0x18, // e
0x00, 0x08, 0x7E, 0x09, 0x02, // f
0x18, 0xA4, 0xA4, 0x9C, 0x78, // g
0x7F, 0x08, 0x04, 0x04, 0x78, // h
0x00, 0x44, 0x7D, 0x40, 0x00, // i
0x20, 0x40, 0x40, 0x3D, 0x00, // j
0x7F, 0x10, 0x28, 0x44, 0x00, // k
0x00, 0x41, 0x7F, 0x40, 0x00, // l
0x7C, 0x04, 0x78, 0x04, 0x78, // m
0x7C, 0x08, 0x04, 0x04, 0x78, // n
0x38, 0x44, 0x44, 0x44, 0x38, // o
0xFC, 0x18, 0x24, 0x24, 0x18, // p
0x18, 0x24, 0x24, 0x18, 0xFC, // q
0x7C, 0x08, 0x04, 0x04, 0x08, // r
0x48, 0x54, 0x54, 0x54, 0x24, // s
0x04, 0x04, 0x3F, 0x44, 0x24, // t
0x3C, 0x40, 0x40, 0x20, 0x7C, // u
0x1C, 0x20, 0x40, 0x20, 0x1C, // v
0x3C, 0x40, 0x30, 0x40, 0x3C, // w
0x44, 0x28, 0x10, 0x28, 0x44, // x
0x4C, 0x90, 0x90, 0x90, 0x7C, // y
0x44, 0x64, 0x54, 0x4C, 0x44, // z
0x00, 0x08, 0x36, 0x41, 0x00,
0x00, 0x00, 0x77, 0x00, 0x00,
0x00, 0x41, 0x36, 0x08, 0x00,
0x02, 0x01, 0x02, 0x04, 0x02,
0x3C, 0x26, 0x23, 0x26, 0x3C,
0x1E, 0xA1, 0xA1, 0x61, 0x12,
0x3A, 0x40, 0x40, 0x20, 0x7A,
0x38, 0x54, 0x54, 0x55, 0x59,
0x21, 0x55, 0x55, 0x79, 0x41,
0x21, 0x54, 0x54, 0x78, 0x41,
0x21, 0x55, 0x54, 0x78, 0x40,
0x20, 0x54, 0x55, 0x79, 0x40,
0x0C, 0x1E, 0x52, 0x72, 0x12,
0x39, 0x55, 0x55, 0x55, 0x59,
0x39, 0x54, 0x54, 0x54, 0x59,
0x39, 0x55, 0x54, 0x54, 0x58,
0x00, 0x00, 0x45, 0x7C, 0x41,
0x00, 0x02, 0x45, 0x7D, 0x42,
0x00, 0x01, 0x45, 0x7C, 0x40,
0xF0, 0x29, 0x24, 0x29, 0xF0,
0xF0, 0x28, 0x25, 0x28, 0xF0,
0x7C, 0x54, 0x55, 0x45, 0x00,
0x20, 0x54, 0x54, 0x7C, 0x54,
0x7C, 0x0A, 0x09, 0x7F, 0x49,
0x32, 0x49, 0x49, 0x49, 0x32,
0x32, 0x48, 0x48, 0x48, 0x32,
0x32, 0x4A, 0x48, 0x48, 0x30,
0x3A, 0x41, 0x41, 0x21, 0x7A,
0x3A, 0x42, 0x40, 0x20, 0x78,
0x00, 0x9D, 0xA0, 0xA0, 0x7D,
0x39, 0x44, 0x44, 0x44, 0x39,
0x3D, 0x40, 0x40, 0x40, 0x3D,
0x3C, 0x24, 0xFF, 0x24, 0x24,
0x48, 0x7E, 0x49, 0x43, 0x66,
0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
0xFF, 0x09, 0x29, 0xF6, 0x20,
0xC0, 0x88, 0x7E, 0x09, 0x03,
0x20, 0x54, 0x54, 0x79, 0x41,
0x00, 0x00, 0x44, 0x7D, 0x41,
0x30, 0x48, 0x48, 0x4A, 0x32,
0x38, 0x40, 0x40, 0x22, 0x7A,
0x00, 0x7A, 0x0A, 0x0A, 0x72,
0x7D, 0x0D, 0x19, 0x31, 0x7D,
0x26, 0x29, 0x29, 0x2F, 0x28,
0x26, 0x29, 0x29, 0x29, 0x26,
0x30, 0x48, 0x4D, 0x40, 0x20,
0x38, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x38,
0x2F, 0x10, 0xC8, 0xAC, 0xBA,
0x2F, 0x10, 0x28, 0x34, 0xFA,
0x00, 0x00, 0x7B, 0x00, 0x00,
0x08, 0x14, 0x2A, 0x14, 0x22,
0x22, 0x14, 0x2A, 0x14, 0x08,
0xAA, 0x00, 0x55, 0x00, 0xAA,
0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x00, 0x00, 0x00, 0xFF, 0x00,
0x10, 0x10, 0x10, 0xFF, 0x00,
0x14, 0x14, 0x14, 0xFF, 0x00,
0x10, 0x10, 0xFF, 0x00, 0xFF,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x14, 0x14, 0x14, 0xFC, 0x00,
0x14, 0x14, 0xF7, 0x00, 0xFF,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x14, 0x14, 0xF4, 0x04, 0xFC,
0x14, 0x14, 0x17, 0x10, 0x1F,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0x1F, 0x00,
0x10, 0x10, 0x10, 0xF0, 0x00,
0x00, 0x00, 0x00, 0x1F, 0x10,
0x10, 0x10, 0x10, 0x1F, 0x10,
0x10, 0x10, 0x10, 0xF0, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0xFF, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x14,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0x1F, 0x10, 0x17,
0x00, 0x00, 0xFC, 0x04, 0xF4,
0x14, 0x14, 0x17, 0x10, 0x17,
0x14, 0x14, 0xF4, 0x04, 0xF4,
0x00, 0x00, 0xFF, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x14, 0xF7, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x17, 0x14,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0xF4, 0x14,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x00, 0x00, 0x1F, 0x10, 0x1F,
0x00, 0x00, 0x00, 0x1F, 0x14,
0x00, 0x00, 0x00, 0xFC, 0x14,
0x00, 0x00, 0xF0, 0x10, 0xF0,
0x10, 0x10, 0xFF, 0x10, 0xFF,
0x14, 0x14, 0x14, 0xFF, 0x14,
0x10, 0x10, 0x10, 0x1F, 0x00,
0x00, 0x00, 0x00, 0xF0, 0x10,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xFF, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0xFF, 0xFF,
0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x38, 0x44, 0x44, 0x38, 0x44,
0x7C, 0x2A, 0x2A, 0x3E, 0x14,
0x7E, 0x02, 0x02, 0x06, 0x06,
0x02, 0x7E, 0x02, 0x7E, 0x02,
0x63, 0x55, 0x49, 0x41, 0x63,
0x38, 0x44, 0x44, 0x3C, 0x04,
0x40, 0x7E, 0x20, 0x1E, 0x20,
0x06, 0x02, 0x7E, 0x02, 0x02,
0x99, 0xA5, 0xE7, 0xA5, 0x99,
0x1C, 0x2A, 0x49, 0x2A, 0x1C,
0x4C, 0x72, 0x01, 0x72, 0x4C,
0x30, 0x4A, 0x4D, 0x4D, 0x30,
0x30, 0x48, 0x78, 0x48, 0x30,
0xBC, 0x62, 0x5A, 0x46, 0x3D,
0x3E, 0x49, 0x49, 0x49, 0x00,
0x7E, 0x01, 0x01, 0x01, 0x7E,
0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
0x44, 0x44, 0x5F, 0x44, 0x44,
0x40, 0x51, 0x4A, 0x44, 0x40,
0x40, 0x44, 0x4A, 0x51, 0x40,
0x00, 0x00, 0xFF, 0x01, 0x03,
0xE0, 0x80, 0xFF, 0x00, 0x00,
0x08, 0x08, 0x6B, 0x6B, 0x08,
0x36, 0x12, 0x36, 0x24, 0x36,
0x06, 0x0F, 0x09, 0x0F, 0x06,
0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x00, 0x10, 0x10, 0x00,
0x30, 0x40, 0xFF, 0x01, 0x01,
0x00, 0x1F, 0x01, 0x01, 0x1E,
0x00, 0x19, 0x1D, 0x17, 0x12,
0x00, 0x3C, 0x3C, 0x3C, 0x3C,
0x00, 0x00, 0x00, 0x00, 0x00,
};
static uint8_t ColStart, RowStart; // some displays need this changed
//static uint8_t Rotation; // 0 to 3
//static enum initRFlags TabColor;
static int16_t _width = ST7735_TFTWIDTH; // this could probably be a constant, except it is used in Adafruit_GFX and depends on image rotation
static int16_t _height = ST7735_TFTHEIGHT;
// The Data/Command pin must be valid when the eighth bit is
// sent. The SSI module has hardware input and output FIFOs
// that are 8 locations deep; however, they are not used in
// this implementation. Each function first stalls while
// waiting for any pending SSI2 transfers to complete. Once
// the SSI2 module is idle, it then prepares the Chip Select
// pin for the LCD and the Data/Command pin. Next it starts
// transmitting the data or command. Finally once the
// hardware is idle again, it sets the chip select pin high
// as required by the serial protocol. This is a
// significant change from previous implementations of this
// function. It is less efficient without the FIFOs, but it
// should ensure that the Chip Select and Data/Command pin
// statuses all match the byte that is actually being
// transmitted.
// NOTE: These functions will crash or stall indefinitely if
// the SSI2 module is not initialized and enabled.
// This is a helper function that sends an 8-bit command to the LCD.
// Inputs: c 8-bit code to transmit
// Outputs: 8-bit reply
// Assumes: SSI2 and ports have already been initialized and enabled
uint8_t static writecommand(uint8_t c) {
// wait until SSI2 not busy/transmit FIFO empty
while((SSI2_SR_R&SSI_SR_BSY)==SSI_SR_BSY){};
TFT_CS = TFT_CS_LOW;
DC = DC_COMMAND;
SSI2_DR_R = c; // data out
while((SSI2_SR_R&SSI_SR_RNE)==0){}; // wait until response
TFT_CS = TFT_CS_HIGH;
return (uint8_t)SSI2_DR_R; // return the response
}
// This is a helper function that sends a piece of 8-bit data to the LCD.
// Inputs: c 8-bit data to transmit
// Outputs: 8-bit reply
// Assumes: SSI2 and ports have already been initialized and enabled
uint8_t static writedata(uint8_t c) {
// wait until SSI2 not busy/transmit FIFO empty
while((SSI2_SR_R&SSI_SR_BSY)==SSI_SR_BSY){};
TFT_CS = TFT_CS_LOW;
DC = DC_DATA;
SSI2_DR_R = c; // data out
while((SSI2_SR_R&SSI_SR_RNE)==0){}; // wait until response
TFT_CS = TFT_CS_HIGH;
return (uint8_t)SSI2_DR_R; // return the response
}
// delay function from sysctl.c
// which delays 3.3*ulCount cycles
// ulCount=23746 => 1ms = 23746*3.3cycle/loop/80,000
// Rather than a bazillion writecommand() and writedata() calls, screen
// initialization commands and arguments are organized in these tables
// stored in ROM. The table may look bulky, but that's mostly the
// formatting -- storage-wise this is hundreds of bytes more compact
// than the equivalent code. Companion function follows.
#define DELAY 0x80
//static const uint8_t
// Bcmd[] = { // Initialization commands for 7735B screens
// 18, // 18 commands in list:
// ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
// 50, // 50 ms delay
// ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
// 255, // 255 = 500 ms delay
// ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
// 0x05, // 16-bit color
// 10, // 10 ms delay
// ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay:
// 0x00, // fastest refresh
// 0x06, // 6 lines front porch
// 0x03, // 3 lines back porch
// 10, // 10 ms delay
// ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
// 0x08, // Row addr/col addr, bottom to top refresh
// ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
// 0x15, // 1 clk cycle nonoverlap, 2 cycle gate
// // rise, 3 cycle osc equalize
// 0x02, // Fix on VTL
// ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
// 0x0, // Line inversion
// ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay:
// 0x02, // GVDD = 4.7V
// 0x70, // 1.0uA
// 10, // 10 ms delay
// ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
// 0x05, // VGH = 14.7V, VGL = -7.35V
// ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
// 0x01, // Opamp current small
// 0x02, // Boost frequency
// ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay:
// 0x3C, // VCOMH = 4V
// 0x38, // VCOML = -1.1V
// 10, // 10 ms delay
// ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
// 0x11, 0x15,
// ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
// 0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
// 0x21, 0x1B, 0x13, 0x19, // these config values represent)
// 0x17, 0x15, 0x1E, 0x2B,
// 0x04, 0x05, 0x02, 0x0E,
// ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay:
// 0x0B, 0x14, 0x08, 0x1E, // (ditto)
// 0x22, 0x1D, 0x18, 0x1E,
// 0x1B, 0x1A, 0x24, 0x2B,
// 0x06, 0x06, 0x02, 0x0F,
// 10, // 10 ms delay
// ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay:
// 0x00, 0x02, // XSTART = 2
// 0x00, 0x81, // XEND = 129
// ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay:
// 0x00, 0x02, // XSTART = 1
// 0x00, 0x81, // XEND = 160
// ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
// 10, // 10 ms delay
// ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
// 255 }; // 255 = 500 ms delay
static const uint8_t
Rcmd1[] = { // Init for 7735R, part 1 (red or green tab)
15, // 15 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
150, // 150 ms delay
ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
255, // 500 ms delay
ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
0x01, 0x2C, 0x2D, // Dot inversion mode
0x01, 0x2C, 0x2D, // Line inversion mode
ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
0x07, // No inversion
ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
0xA2,
0x02, // -4.6V
0x84, // AUTO mode