-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTS_HAL.c
1548 lines (1466 loc) · 61.4 KB
/
CTS_HAL.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
/*******************************************************************************
* CTS_HAL.c - Hardware abstraction of various combinations of modules to
* perform a capacitance measurement.
*
* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
*
* 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.
*
* Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 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
* OWNER 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.
*
******************************************************************************/
/***************************************************************************//**
* @file CTS_HAL.c
*
* @brief
*
* @par Project:
* MSP430 Capacitive Touch Library
*
* @par Developed using:
* IAR Version : 5.10.6 [Kickstart] (5.10.6.30180)
* CCS Version : 4.2.1.00004, w/support for GCC extensions (--gcc)
*
*
* @version 1.0.0 Initial Release
*
* @par Supported Hardware Configurations:
* - TI_CTS_RO_COMPAp_TA0_WDTp_HAL()
* - TI_CTS_fRO_COMPAp_TA0_SW_HAL()
* - TI_CTS_fRO_COMPAp_SW_TA0_HAL()
* - TI_CTS_RO_COMPAp_TA1_WDTp_HAL()
* - TI_CTS_fRO_COMPAp_TA1_SW_HAL()
* - TI_CTS_RC_PAIR_TA0_HAL()
* - TI_CTS_RO_PINOSC_TA0_WDTp_HAL()
* - TI_CTS_RO_PINOSC_TA0_HAL()
* - TI_CTS_fRO_PINOSC_TA0_SW_HAL()
* - TI_CTS_RO_COMPB_TA0_WDTA_HAL()
* - TI_CTS_RO_COMPB_TA1_WDTA_HAL()
* - TI_CTS_fRO_COMPB_TA0_SW_HAL()
* - TI_CTS_fRO_COMPB_TA1_SW_HAL()
******************************************************************************/
/***************************************************************************//**
* @addtogroup CTS_HAL
* @{
******************************************************************************/
#include "CTS_HAL.h"
#ifdef RO_COMPAp_TA0_WDTp
/***************************************************************************//**
* @brief RO method capactiance measurement using CompA+, TimerA0, and WDT+
*
* \n Schematic Description of CompA+ forming relaxation oscillator and
* coupling (connection) between the relaxation oscillator and TimerA0.
* \n <- Output
* \n -> Input
* \n R Resistor (typically 100Kohms)
* \n
* \n +-<-Px.y (reference)
* \n |
* \n R
* \n |
* \n +---+-->COMPA+
* \n | |
* \n R R
* \n | |
* \n GND |
* \n |
* \n +-->TACLK
* \n |
* \n element-+-R--+-<-CAOUT
* \n |
* \n +------->COMPA-
* \n
* \n The WDT+ interval represents the measurement window. The number of
* counts within the TA0R that have accumulated during the measurement
* window represents the capacitance of the element.
*
* @param group Address of the structure describing the Sensor to be measured
* @param counts Address to where the measurements are to be written
* @return none
******************************************************************************/
void TI_CTS_RO_COMPAp_TA0_WDTp_HAL(const struct Sensor *group, uint16_t *counts)
{
uint8_t i;
//** Context Save
// Status Register:
// WDTp: IE1, WDTCTL
// TIMERA0: TACTL, TACCTL1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
uint8_t contextSaveSR;
uint8_t contextSaveIE1;
uint16_t contextSaveWDTCTL;
uint16_t contextSaveTACTL,contextSaveTACCTL1,contextSaveTACCR1;
uint8_t contextSaveCACTL1,contextSaveCACTL2,contextSaveCAPD;
uint8_t contextSaveCaoutDir,contextSaveCaoutSel;
uint8_t contextSavetxclkDir,contextSavetxclkSel;
uint8_t contextSaveRefDir,contextSaveRefOutSel;
#ifdef SEL2REGISTER
uint8_t contextSaveCaoutSel2,contextSaveTxclkSel2;
contextSaveCaoutSel2 = *(group->caoutSel2Register);
contextSaveTxclkSel2 = *(group->txclkSel2Register);
#endif
contextSaveSR = __get_SR_register();
contextSaveIE1 = IE1;
contextSaveWDTCTL = WDTCTL;
contextSaveWDTCTL &= 0x00FF;
contextSaveWDTCTL |= WDTPW;
contextSaveTACTL = TACTL;
contextSaveTACCTL1 = TACCTL1;
contextSaveTACCR1 = TACCR1;
contextSaveCACTL1 = CACTL1;
contextSaveCACTL2 = CACTL2;
contextSaveCAPD = CAPD;
contextSaveCaoutDir = *(group->caoutDirRegister);
contextSaveCaoutSel = *(group->caoutSelRegister);
contextSavetxclkDir = *(group->txclkDirRegister);
contextSavetxclkSel = *(group->txclkSelRegister);
contextSaveRefDir = *(group->refPxdirRegister);
contextSaveRefOutSel = *(group->refPxoutRegister);
TACTL = TASSEL_0+MC_2; // TACLK, cont mode
TACCTL1 = CM_3+CCIS_2+CAP; // Pos&Neg,GND,Cap
*(group->caoutDirRegister) |= group->caoutBits;
*(group->txclkDirRegister) &= ~group->txclkBits;
*(group->caoutSelRegister) |= group->caoutBits;
*(group->txclkSelRegister) |= group->txclkBits;
#ifdef SEL2REGISTER
*(group->caoutSel2Register) |= group->caoutBits;
*(group->txclkSel2Register) |= group->txclkBits;
#endif
*(group->refPxdirRegister) |= group->refBits;
*(group->refPxoutRegister) |= group->refBits;
CACTL1 |= CAON; // Turn on comparator
CAPD |= (group->capdBits);
IE1 |= WDTIE; // enable WDT interrupt
for (i = 0; i<(group->numElements); i++)
{
CACTL2= group->refCactl2Bits + (group->arrayPtr[i])->inputBits;
//** Setup Gate Timer *****************************************************
// Set duration of sensor measurment
WDTCTL = WDTPW+WDTTMSEL+ group->measGateSource + group->accumulationCycles;
TACTL |= TACLR; // Clear Timer_A TAR
if(group->measGateSource == GATE_WDTp_ACLK)
{
__bis_SR_register(LPM3_bits+GIE); // Wait for WDT interrupt
}
else
{
__bis_SR_register(LPM0_bits+GIE); // Wait for WDT interrupt
}
TACCTL1 ^= CCIS0; // Create SW capture of CCR1
counts[i] = TACCR1; // Save result
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
}
// End Sequence
//** Context Restore
// WDTp: IE1, WDCTL
// TIMERA0: TACTL, TACCTL1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
#ifdef SEL2REGISTER
*(group->caoutSel2Register) = contextSaveCaoutSel2;
*(group->txclkSel2Register) = contextSaveTxclkSel2;
#endif
__bis_SR_register(contextSaveSR);
if(!(contextSaveSR & GIE))
{
__bic_SR_register(GIE); // Wait for WDT interrupt
}
IE1 = contextSaveIE1;
WDTCTL = contextSaveWDTCTL;
TACTL = contextSaveTACTL;
TACCTL1 = contextSaveTACCTL1;
TACCR1 = contextSaveTACCR1;
CACTL1 = contextSaveCACTL1;
CACTL2 = contextSaveCACTL2;
CAPD = contextSaveCAPD;
*(group->caoutDirRegister) = contextSaveCaoutDir;
*(group->caoutSelRegister) = contextSaveCaoutSel;
*(group->txclkDirRegister) = contextSavetxclkDir;
*(group->txclkSelRegister) = contextSavetxclkSel;
*(group->refPxdirRegister) = contextSaveRefDir;
*(group->refPxoutRegister) = contextSaveRefOutSel;
}
#endif
#ifdef fRO_COMPAp_TA0_SW
/***************************************************************************//**
* @brief RO method capactiance measurement using CompA+, TimerA0, and SW loop
*
* \n Schematic Description of CompA+ forming relaxation oscillator.
* \n <- Output
* \n -> Input
* \n R Resistor (typically 100Kohms)
* \n
* \n +-<-Px.y (reference)
* \n |
* \n R
* \n |
* \n +---+-->COMPA+
* \n | |
* \n R R
* \n | |
* \n GND |
* \n |
* \n +-->TACLK
* \n |
* \n element-+-R--+-<-CAOUT
* \n |
* \n +------->COMPA-
* \n
* \n The timer counts to TA0CCR0 representing the measurement window. The
* number of counts within the SW loop that have accumulated during the
* measurement window represents the capacitance of the element.
*
* @param group Address of the structure describing the Sensor to be measured
* @param counts Address to where the measurements are to be written
* @return none
******************************************************************************/
void TI_CTS_fRO_COMPAp_TA0_SW_HAL(const struct Sensor *group, uint16_t *counts)
{
uint8_t i;
uint16_t j;
//** Context Save
// Status Register:
// TIMERA0: TACTL, TACCTL0
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
uint16_t contextSaveTACTL,contextSaveTACCTL0,contextSaveTACCR0;
uint8_t contextSaveCACTL1,contextSaveCACTL2,contextSaveCAPD;
uint8_t contextSaveCaoutDir,contextSaveCaoutSel;
uint8_t contextSavetxclkDir,contextSavetxclkSel;
uint8_t contextSaveRefDir,contextSaveRefOutSel;
#ifdef SEL2REGISTER
uint8_t contextSaveCaoutSel2,contextSaveTxclkSel2;
contextSaveCaoutSel2 = *(group->caoutSel2Register);
contextSaveTxclkSel2 = *(group->txclkSel2Register);
#endif
contextSaveTACTL = TACTL;
contextSaveTACCTL0 = TACCTL0;
contextSaveTACCR0 = TACCR0;
contextSaveCACTL1 = CACTL1;
contextSaveCACTL2 = CACTL2;
contextSaveCAPD = CAPD;
contextSaveCaoutDir = *(group->caoutDirRegister);
contextSaveCaoutSel = *(group->caoutSelRegister);
contextSavetxclkDir = *(group->txclkDirRegister);
contextSavetxclkSel = *(group->txclkSelRegister);
contextSaveRefDir = *(group->refPxdirRegister);
contextSaveRefOutSel = *(group->refPxoutRegister);
//** Setup Measurement timer***************************************************
// Configure Timer TA0
TACCR0 =(group->accumulationCycles);
TACCTL0 &= ~CAP;
// setup connections between CAOUT and TA0
*(group->caoutDirRegister) |= group->caoutBits;
*(group->txclkDirRegister) &= ~group->txclkBits;
*(group->caoutSelRegister) |= group->caoutBits;
*(group->txclkSelRegister) |= group->txclkBits;
#ifdef SEL2REGISTER
*(group->caoutSel2Register) |= group->caoutBits;
*(group->txclkSel2Register) |= group->txclkBits;
#endif
// setup reference
*(group->refPxdirRegister) |= group->refBits;
*(group->refPxoutRegister) |= group->refBits;
CACTL1 |= CAON; // Turn on comparator
CAPD |= (group->capdBits);
for (i = 0; i<(group->numElements); i++)
{
j=0;
CACTL2= group->refCactl2Bits + (group->arrayPtr[i])->inputBits;
//** Setup Gate Timer **************
// Set duration of sensor measurment
TACTL = TASSEL_0+TACLR+MC_1; // TACLK, reset, up mode
TACTL &= ~TAIFG; // clear IFG
while(!(TACTL & TAIFG))
{
j++;
} // end accumulation
counts[i] = j;
}
// End Sequence
//** Context Restore
// TIMERA0: TACTL, TACCTL1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
#ifdef SEL2REGISTER
*(group->caoutSel2Register) = contextSaveCaoutSel2;
*(group->txclkSel2Register) = contextSaveTxclkSel2;
#endif
TACTL = contextSaveTACTL;
TACCTL0 = contextSaveTACCTL0;
TACCR0 = contextSaveTACCR0;
CACTL1 = contextSaveCACTL1;
CACTL2 = contextSaveCACTL2;
CAPD = contextSaveCAPD;
*(group->caoutDirRegister) = contextSaveCaoutDir;
*(group->caoutSelRegister) = contextSaveCaoutSel;
*(group->txclkDirRegister) = contextSavetxclkDir;
*(group->txclkSelRegister) = contextSavetxclkSel;
*(group->refPxdirRegister) = contextSaveRefDir;
*(group->refPxoutRegister) = contextSaveRefOutSel;
}
#endif
#ifdef fRO_COMPAp_SW_TA0
/***************************************************************************//**
* @brief RO method capactiance measurement using CompA+, TimerA0, and SW loop
*
* \n Schematic Description of CompA+ forming relaxation oscillator.
* \n <- Output
* \n -> Input
* \n R Resistor (typically 100Kohms)
* \n
* \n +-<-Px.y (reference)
* \n |
* \n R
* \n |
* \n +---+-->COMPA+
* \n | |
* \n R R
* \n | |
* \n GND |
* \n |
* \n |
* \n element-+-R--+-<-CAOUT
* \n |
* \n +------->COMPA-
* \n
* \n The SW loop counts to 'n' accumulationCycles, representing the
* \n measurement window. The number of timer counts within TA0R register
* \n represents the capacitance of the element.
*
* @param group Address of the structure describing the Sensor to be measured
* @param counts Address to where the measurements are to be written
* @return none
******************************************************************************/
void TI_CTS_fRO_COMPAp_SW_TA0_HAL(const struct Sensor *group, uint16_t *counts)
{
uint8_t i;
uint16_t j;
//** Context Save
// Status Register:
// TIMERA0: TACTL, TACCTL0, TACCTL1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, caoutSel2, refout, refdir
uint16_t contextSaveTACTL,contextSaveTACCTL0,contextSaveTACCTL1;
uint16_t contextSaveTACCR0,contextSaveTACCR1;
uint8_t contextSaveCACTL1,contextSaveCACTL2,contextSaveCAPD;
uint8_t contextSaveCaoutDir,contextSaveCaoutSel;
uint8_t contextSaveRefDir,contextSaveRefOutSel;
#ifdef SEL2REGISTER
uint8_t contextSaveCaoutSel2,contextSaveTxclkSel2;
contextSaveCaoutSel2 = *(group->caoutSel2Register);
#endif
contextSaveTACTL = TACTL;
contextSaveTACCTL0 = TACCTL0;
contextSaveTACCTL1 = TACCTL1;
contextSaveTACCR0 = TACCR0;
contextSaveTACCR1 = TACCR1;
contextSaveCACTL1 = CACTL1;
contextSaveCACTL2 = CACTL2;
contextSaveCAPD = CAPD;
contextSaveCaoutDir = *(group->caoutDirRegister);
contextSaveCaoutSel = *(group->caoutSelRegister);
contextSaveRefDir = *(group->refPxdirRegister);
contextSaveRefOutSel = *(group->refPxoutRegister);
//** Setup Measurement timer***************************************************
// Configure Timer TA0
TACCTL0 = CM_3+CCIS_2+CAP; // Pos&Neg,GND,Cap
TACCTL1 = CM_3+CCIS_2+CAP; // Pos&Neg,GND,Cap
// setup connections between CAOUT and TA0
*(group->caoutDirRegister) |= group->caoutBits;
*(group->caoutSelRegister) |= group->caoutBits;
#ifdef SEL2REGISTER
*(group->caoutSel2Register) |= group->caoutBits;
#endif
// setup reference
*(group->refPxdirRegister) |= group->refBits;
*(group->refPxoutRegister) |= group->refBits;
CACTL1 |= CAON; // Turn on comparator
CAPD |= (group->capdBits);
for (i = 0; i<(group->numElements); i++)
{
CACTL2= group->refCactl2Bits + (group->arrayPtr[i])->inputBits;
//** Setup Gate Timer **************
// Set duration of sensor measurment
TACTL = group->measureGateSource+group->sourceScale+TACLR+MC_2;
TACCTL0 ^= CCIS0; // Create SW capture of CCR0
for(j = group->accumulationCycles; j > 0; j--)
{
CACTL1 &= ~CAIFG;
while(!(CACTL1 & CAIFG));
}
TACCTL1 ^= CCIS0; // Create SW capture of CCR1
counts[i] = TACCR1; // Save result
counts[i] -= TACCR0; // Save result
TACCTL0 &= ~CCIFG;
TACCTL1 &= ~CCIFG;
}
// End Sequence
//** Context Restore
// WDTp: IE1, WDCTL
// TIMERA0: TACTL, TACCTL0, TACCTL1, TACCR0, TACCR1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, caoutSel2, refout, refdir
#ifdef SEL2REGISTER
*(group->caoutSel2Register) = contextSaveCaoutSel2;
#endif
TACTL = contextSaveTACTL;
TACCTL0 = contextSaveTACCTL0;
TACCTL1 = contextSaveTACCTL1;
TACCR0 = contextSaveTACCR0;
TACCR1 = contextSaveTACCR1;
CACTL1 = contextSaveCACTL1;
CACTL2 = contextSaveCACTL2;
CAPD = contextSaveCAPD;
*(group->caoutDirRegister) = contextSaveCaoutDir;
*(group->caoutSelRegister) = contextSaveCaoutSel;
*(group->refPxdirRegister) = contextSaveRefDir;
*(group->refPxoutRegister) = contextSaveRefOutSel;
}
#endif
#ifdef RO_COMPAp_TA1_WDTp
/***************************************************************************//**
* @brief RO method capactiance measurement using CompA+, TimerA1, and WDT+
*
* Schematic Description of CompA+ forming relaxation oscillator and
* coupling (connection) between the relaxation oscillator and TimerA0.
* \n <- Output
* \n -> Input
* \n R Resistor (typically 100Kohms)
* \n
* \n +-<-Px.y (reference)
* \n |
* \n R
* \n |
* \n +---+-->COMPA+
* \n | |
* \n R R
* \n | |
* \n GND |
* \n |
* \n +-->TA1CLK
* \n |
* \n element-+-R--+-<-CAOUT
* \n |
* \n +------->COMPA-
* \n
* The WDT+ interval represents the measurement window. The number of
* counts within the TA0R that have accumulated during the measurement
* window represents the capacitance of the element.
*
* @param group Address of the structure describing the Sensor to be measured
* @param counts Address to where the measurements are to be written
* @return none
******************************************************************************/
void TI_CTS_RO_COMPAp_TA1_WDTp_HAL(const struct Sensor *group, uint16_t *counts)
{
uint8_t i;
//** Context Save
// Status Register:
// WDTp: IE1, WDTCTL
// TIMERA0: TA1CTL, TA1CCTL1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
uint8_t contextSaveSR;
uint8_t contextSaveIE1;
uint16_t contextSaveWDTCTL;
uint16_t contextSaveTA1CTL,contextSaveTA1CCTL1;
uint16_t contextSaveTA1CCR1;
uint8_t contextSaveCACTL1,contextSaveCACTL2,contextSaveCAPD;
uint8_t contextSaveCaoutDir,contextSaveCaoutSel;
uint8_t contextSavetxclkDir,contextSavetxclkSel;
uint8_t contextSaveRefDir,contextSaveRefOutSel;
#ifdef SEL2REGISTER
uint8_t contextSaveCaoutSel2,contextSaveTxclkSel2;
contextSaveCaoutSel2 = *(group->caoutSel2Register);
contextSaveTxclkSel2 = *(group->txclkSel2Register);
#endif
contextSaveSR = __get_SR_register();
contextSaveIE1 = IE1;
contextSaveWDTCTL = WDTCTL;
contextSaveWDTCTL &= 0x00FF;
contextSaveWDTCTL |= WDTPW;
contextSaveTA1CTL = TA1CTL;
contextSaveTA1CCTL1 = TA1CCTL1;
contextSaveTA1CCR1 = TA1CCR1;
contextSaveCACTL1 = CACTL1;
contextSaveCACTL2 = CACTL2;
contextSaveCAPD = CAPD;
contextSaveCaoutDir = *(group->caoutDirRegister);
contextSaveCaoutSel = *(group->caoutSelRegister);
contextSavetxclkDir = *(group->txclkDirRegister);
contextSavetxclkSel = *(group->txclkSelRegister);
contextSaveRefDir = *(group->refPxdirRegister);
contextSaveRefOutSel = *(group->refPxoutRegister);
//** Setup Measurement timer***************************************************
// Choices are TA0,TA1,TB0,TB1,TD0,TD1 these choices are pushed up into the
// capacitive touch layer.
TA1CTL = TASSEL_0+MC_2; // TA1CLK, cont mode
TA1CCTL1 = CM_3+CCIS_2+CAP; // Pos&Neg,GND,Cap
*(group->caoutDirRegister) |= group->caoutBits;
*(group->txclkDirRegister) &= ~group->txclkBits;
*(group->caoutSelRegister) |= group->caoutBits;
*(group->txclkSelRegister) |= group->txclkBits;
#ifdef SEL2REGISTER
*(group->caoutSel2Register) |= group->caoutBits;
*(group->txclkSel2Register) |= group->txclkBits;
#endif
*(group->refPxdirRegister) |= group->refBits;
*(group->refPxoutRegister) |= group->refBits;
CACTL1 |= CAON; // Turn on comparator
CAPD |= (group->capdBits);
IE1 |= WDTIE; // enable WDT interrupt
for (i = 0; i<(group->numElements); i++)
{
CACTL2= group->refCactl2Bits + (group->arrayPtr[i])->inputBits;
//** Setup Gate Timer *****************************************************
// Set duration of sensor measurment
WDTCTL = WDTPW+WDTTMSEL+ group->measGateSource + group->accumulationCycles;
TA1CTL |= TACLR; // Clear Timer_A TAR
if(group->measGateSource == GATE_WDTp_ACLK)
{
__bis_SR_register(LPM3_bits+GIE); // Wait for WDT interrupt
}
else
{
__bis_SR_register(LPM0_bits+GIE); // Wait for WDT interrupt
}
TA1CCTL1 ^= CCIS0; // Create SW capture of CCR1
counts[i] = TA1CCR1; // Save result
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
}
// End Sequence
//** Context Restore
// WDTp: IE1, WDCTL
// TIMERA0: TACTL, TACCTL1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
#ifdef SEL2REGISTER
*(group->caoutSel2Register) = contextSaveCaoutSel2;
*(group->txclkSel2Register) = contextSaveTxclkSel2;
#endif
__bis_SR_register(contextSaveSR);
if(!(contextSaveSR & GIE))
{
__bic_SR_register(GIE); // Wait for WDT interrupt
}
IE1 = contextSaveIE1;
WDTCTL = contextSaveWDTCTL;
TA1CTL = contextSaveTA1CTL;
TA1CCTL1 = contextSaveTA1CCTL1;
TA1CCR1 = contextSaveTA1CCR1;
CACTL1 = contextSaveCACTL1;
CACTL2 = contextSaveCACTL2;
CAPD = contextSaveCAPD;
*(group->caoutDirRegister) = contextSaveCaoutDir;
*(group->caoutSelRegister) = contextSaveCaoutSel;
*(group->txclkDirRegister) = contextSavetxclkDir;
*(group->txclkSelRegister) = contextSavetxclkSel;
*(group->refPxdirRegister) = contextSaveRefDir;
*(group->refPxoutRegister) = contextSaveRefOutSel;
}
#endif
#ifdef fRO_COMPAp_TA1_SW
/***************************************************************************//**
* @brief RO method capactiance measurement using CompA+, TimerA1, and SW loop
*
* Schematic Description of CompA+ forming relaxation oscillator.
* \n <- Output
* \n -> Input
* \n R Resistor (typically 100Kohms)
* \n
* \n +-<-Px.y (reference)
* \n |
* \n R
* \n |
* \n +---+-->COMPA+
* \n | |
* \n R R
* \n | |
* \n GND |
* \n |
* \n +-->TA1CLK
* \n |
* \n element-+-R--+-<-CAOUT
* \n |
* \n +------->COMPA-
* \n
* \n The timer counts to TA1CCR0 representing the measurement window. The
* number of counts within the SW loop that have accumulated during the
* measurement window represents the capacitance of the element.
*
* @param group Address of the structure describing the Sensor to be measured
* @param counts Address to where the measurements are to be written
* @return none
******************************************************************************/
void TI_CTS_fRO_COMPAp_TA1_SW_HAL(const struct Sensor *group, uint16_t *counts)
{
uint8_t i;
uint16_t j;
//** Context Save
// Status Register:
// TIMERA0: TA1CTL, TA1CCTL0
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
uint16_t contextSaveTA1CTL,contextSaveTA1CCTL0,contextSaveTA1CCR0;
uint8_t contextSaveCACTL1,contextSaveCACTL2,contextSaveCAPD;
uint8_t contextSaveCaoutDir,contextSaveCaoutSel;
uint8_t contextSavetxclkDir,contextSavetxclkSel;
uint8_t contextSaveRefDir,contextSaveRefOutSel;
#ifdef SEL2REGISTER
uint8_t contextSaveCaoutSel2,contextSaveTxclkSel2;
contextSaveCaoutSel2 = *(group->caoutSel2Register);
contextSaveTxclkSel2 = *(group->txclkSel2Register);
#endif
contextSaveTA1CTL = TA1CTL;
contextSaveTA1CCTL0 = TA1CCTL0;
contextSaveTA1CCR0 = TA1CCR0;
contextSaveCACTL1 = CACTL1;
contextSaveCACTL2 = CACTL2;
contextSaveCAPD = CAPD;
contextSaveCaoutDir = *(group->caoutDirRegister);
contextSaveCaoutSel = *(group->caoutSelRegister);
contextSavetxclkDir = *(group->txclkDirRegister);
contextSavetxclkSel = *(group->txclkSelRegister);
contextSaveRefDir = *(group->refPxdirRegister);
contextSaveRefOutSel = *(group->refPxoutRegister);
//** Setup Measurement timer***************************************************
// Configure Timer TA0
TA1CCR0 =(group->accumulationCycles);
// setup connections between CAOUT and TA0
*(group->caoutDirRegister) |= group->caoutBits;
*(group->txclkDirRegister) &= ~group->txclkBits;
*(group->caoutSelRegister) |= group->caoutBits;
*(group->txclkSelRegister) |= group->txclkBits;
#ifdef SEL2REGISTER
*(group->caoutSel2Register) |= group->caoutBits;
*(group->txclkSel2Register) |= group->txclkBits;
#endif
// setup reference
*(group->refPxdirRegister) |= group->refBits;
*(group->refPxoutRegister) |= group->refBits;
CACTL1 |= CAON; // Turn on comparator
CAPD |= (group->capdBits);
for (i = 0; i<(group->numElements); i++)
{
j=0;
CACTL2= group->refCactl2Bits + (group->arrayPtr[i])->inputBits;
//** Setup Gate Timer **************
// Set duration of sensor measurment
TA1CTL = TASSEL_0+TACLR+MC_1; // TA1CLK, Reset, up mode
TA1CTL &= ~TAIFG; // clear IFG
while(!(TACTL & TAIFG))
{
j++;
} // end accumulation
counts[i] = j;
}
// End Sequence
//** Context Restore
// WDTp: IE1, WDCTL
// TIMERA0: TACTL, TACCTL1
// COMPAp: CACTL1, CACTL2, CAPD
// Ports: caoutDIR, caoutSel, txclkDIR, txclkSel, caoutSel2, txclkSel2, refout, refdir
#ifdef SEL2REGISTER
*(group->caoutSel2Register) = contextSaveCaoutSel2;
*(group->txclkSel2Register) = contextSaveTxclkSel2;
#endif
TA1CTL = contextSaveTA1CTL;
TA1CCTL0 = contextSaveTA1CCTL0;
TA1CCR0 = contextSaveTA1CCR0;
CACTL1 = contextSaveCACTL1;
CACTL2 = contextSaveCACTL2;
CAPD = contextSaveCAPD;
*(group->caoutDirRegister) = contextSaveCaoutDir;
*(group->caoutSelRegister) = contextSaveCaoutSel;
*(group->txclkDirRegister) = contextSavetxclkDir;
*(group->txclkSelRegister) = contextSavetxclkSel;
*(group->refPxdirRegister) = contextSaveRefDir;
*(group->refPxoutRegister) = contextSaveRefOutSel;
}
#endif
/***************************************************************************//**
* @brief RC method capactiance measurement using a Pair of GPIO and TimerA0
*
* Schematic Description of two GPIO forming RC measruement.
* \n <- Output
* \n -> Input
* \n R Resistor (typically 1Mohms)
* \n
* \n +-<-Px.y (reference)
* \n |
* \n R
* \n |
* \n Element---+-->Pa.b
* \n
* \n Charge and Discharge Cycle
* \n +
* \n + +
* \n + +
* \n + +
* \n + +
* \n Start Timer After n cycles Stop Timer
* \n The TAR reister value is the number of SMCLK periods within n charge
* and discharge cycles. This value is directly proportional to the
* capacitance of the element measured. 'n' is defined by the variable
* accumulation_cycles.
*
* @param group Address of the structure describing the Sensor to be measured
* @param counts Address to where the measurements are to be written
* @return none
******************************************************************************/
#ifdef RC_PAIR_TA0
void TI_CTS_RC_PAIR_TA0_HAL(const struct Sensor *group,uint16_t *counts)
{
uint8_t i;
uint16_t j;
//** Context Save
// TIMERA0: TA0CTL
// Port: inputPxout, inputPxdir, referencePxout, referencePxdir
uint8_t contextSaveinputPxout,contextSaveinputPxdir,contextSavereferencePxout;
uint8_t contextSavereferencePxdir;
#ifdef __MSP430_HAS_SFR__
uint16_t contextSaveTA0CTL,contextSaveTA0CCR0;
contextSaveTA0CTL = TA0CTL;
contextSaveTA0CCR0 = TA0CCR0;
#else
uint16_t contextSaveTACTL,contextSaveTACCR0;
contextSaveTACTL = TACTL;
contextSaveTACCR0 = TACCR0;
#endif
//** Setup Measurement timer****************************************************
// Choices are TA0,TA1,TB0,TB1,TD0,TD1 these choices are pushed up into the
// capacitive touch layer.
#ifdef __MSP430_HAS_SFR__
TA0CCR0 = 0xFFFF;
#else
TACCR0 = 0xFFFF;
#endif
for (i = 0; i<(group->numElements); i++)
{
// Context Save
contextSaveinputPxout = *((group->arrayPtr[i])->inputPxoutRegister);
contextSaveinputPxdir = *((group->arrayPtr[i])->inputPxdirRegister);
contextSavereferencePxout = *((group->arrayPtr[i])->referencePxoutRegister);
contextSavereferencePxdir = *((group->arrayPtr[i])->referencePxdirRegister);
j = (group->accumulationCycles);
#ifdef __MSP430_HAS_SFR__
TA0CTL = TASSEL_2+TACLR; // SMCLK, up mode
#else
TACTL = TASSEL_2+TACLR; // SMCLK, up mode
#endif
while(j--)
{
//******************************************************************************
// Positive cycle
// SENSOR ---+---- Input (low to high)
// R
// +---- Rerefence (high)
//******************************************************************************
// Input low
*((group->arrayPtr[i])->inputPxoutRegister) &= ~((group->arrayPtr[i])->inputBits);
*((group->arrayPtr[i])->inputPxdirRegister) |= (group->arrayPtr[i])->inputBits;
// Reference High
*((group->arrayPtr[i])->referencePxdirRegister) |= (group->arrayPtr[i])->referenceBits;
*((group->arrayPtr[i])->referencePxoutRegister) |= ((group->arrayPtr[i])->referenceBits);
// Wait until low
while((*((group->arrayPtr[i])->inputPxinRegister)) & ((group->arrayPtr[i])->inputBits));
// Change to an input
*((group->arrayPtr[i])->inputPxdirRegister) &= ~(group->arrayPtr[i])->inputBits;
//**************************************************************************
// This mechanism is traditianally an LPM with the ISR calculating the
// delta between when the first snapshot and the ISR event. If this is
// included within the library the entire port ISR would not be available
// to the calling application. In this example the polling is done with the
// CPU at expense of power and MIPS but preserves the port ISR for other
// interruptible functions.
//**************************************************************************
#ifdef __MSP430_HAS_SFR__
TA0CTL |= MC_1; // start timer
#else
TACTL |= MC_1; // start timer
#endif
//wait until voltage reaches Vih of port
while(!((*((group->arrayPtr[i])->inputPxinRegister) & (group->arrayPtr[i])->inputBits)));
#ifdef __MSP430_HAS_SFR__
TA0CTL &= ~ MC_3; // stop timer
#else
TACTL &= ~ MC_3; // stop timer
#endif
//******************************************************************************
// Negative cycle
// SENSOR ---+---- Input (high to low)
// R
// +---- Rerefence (low)
//******************************************************************************
// Input High
*((group->arrayPtr[i])->inputPxoutRegister) |= ((group->arrayPtr[i])->inputBits);
*((group->arrayPtr[i])->inputPxdirRegister) |= (group->arrayPtr[i])->inputBits;
// Reference Low
*((group->arrayPtr[i])->referencePxoutRegister) &= ~((group->arrayPtr[i])->referenceBits);
// Change to an input
*((group->arrayPtr[i])->inputPxdirRegister) &= ~((group->arrayPtr[i])->inputBits);
#ifdef __MSP430_HAS_SFR__
TA0CTL |= MC_1; // start timer
#else
TACTL |= MC_1; // start timer
#endif
//wait until voltage reaches Vil of port
while((*((group->arrayPtr[i])->inputPxinRegister)) & ((group->arrayPtr[i])->inputBits));
#ifdef __MSP430_HAS_SFR__
TA0CTL &= ~ MC_3; // stop timer
#else
TACTL &= ~ MC_3; // stop timer
#endif
} // END accumulation loop for a single element
#ifdef __MSP430_HAS_SFR__
counts[i] = TA0R;
#else
counts[i] = TAR;
#endif
// Context Restore
*((group->arrayPtr[i])->inputPxoutRegister) = contextSaveinputPxout;
*((group->arrayPtr[i])->inputPxdirRegister) = contextSaveinputPxdir;
*((group->arrayPtr[i])->referencePxoutRegister) = contextSavereferencePxout;
*((group->arrayPtr[i])->referencePxdirRegister) = contextSavereferencePxdir;
} // END FOR loop which cycles through elements within sensor
//** Context Restore
#ifdef __MSP430_HAS_SFR__
TA0CTL = contextSaveTA0CTL;
TA0CCR0 = contextSaveTA0CCR0;
#else
TACTL = contextSaveTACTL;
TACCR0 = contextSaveTACCR0;
#endif
}
#endif
/***************************************************************************//**
* @brief fRO method capactiance measurement using the PinOsc and TimerA0
*
* \n Charge and Discharge Cycle
* \n +
* \n + +
* \n + +
* \n + +
* \n + +
* \n Start Timer After n cycles Stop Timer
* \n The TAR reister value is the number of SW loops (function of MCLK)
* within n charge and discharge cycles. This value is directly
* proportional to the capacitance of the element measured. 'n' is
* defined by the variable accumulation_cycles.
*
* @param group Address of the structure describing the Sensor to be measured
* @param counts Address to where the measurements are to be written
* @return none
******************************************************************************/
#ifdef fRO_PINOSC_TA0_SW
void TI_CTS_fRO_PINOSC_TA0_SW_HAL(const struct Sensor *group,uint16_t *counts)
{
uint8_t i;
uint16_t j;
//** Context Save
// TIMERA0: TA0CTL
// Ports: PxSEL, PxSEL2
uint16_t contextSaveTA0CTL, contextSaveTA0CCTL0;
uint8_t contextSaveSel,contextSaveSel2;
contextSaveTA0CTL = TA0CTL;
contextSaveTA0CCTL0 = TA0CCTL0;
// Setup Measurement timer
TACCR0 =(group->accumulationCycles);
for (i =0; i< (group->numElements); i++)
{
j = 0;
// Context Save
contextSaveSel = *((group->arrayPtr[i])->inputPxselRegister);
contextSaveSel2 = *((group->arrayPtr[i])->inputPxsel2Register);
// start single oscillation (rise then fall and trigger on fall)
*((group->arrayPtr[i])->inputPxselRegister) &= ~((group->arrayPtr[i])->inputBits);
*((group->arrayPtr[i])->inputPxsel2Register) |= ((group->arrayPtr[i])->inputBits);
TA0CTL = TASSEL_3+TACLR+MC_1; // INCLK, reset, up mode
TA0CTL &= ~TAIFG; // clear IFG
// start timer in up mode
while(!(TA0CTL & TAIFG))
{
j++;
} // end accumulation
counts[i] = j;
TA0CTL &= ~MC_1;
// Context Restore
*((group->arrayPtr[i])->inputPxselRegister) = contextSaveSel;
*((group->arrayPtr[i])->inputPxsel2Register) = contextSaveSel2;
}
// End Sequence
// Context Restore
TA0CTL = contextSaveTA0CTL;
TA0CCTL0 = contextSaveTA0CCTL0;
}
#endif
#ifdef RO_PINOSC_TA0_WDTp
/***************************************************************************//**
* @brief RO method capactiance measurement using PinOsc IO, TimerA0, and WDT+
*
* Schematic Description:
*
* \n element-----+->Px.y
*
* \n The WDT+ interval represents the measurement window. The number of
* counts within the TA0R that have accumulated during the measurement
* window represents the capacitance of the element.
*
* @param group Pointer to the structure describing the Sensor to be measured
* @param counts Pointer to where the measurements are to be written
* @return none
******************************************************************************/
void TI_CTS_RO_PINOSC_TA0_WDTp_HAL(const struct Sensor *group,uint16_t *counts)
{
uint8_t i;
//** Context Save
// Status Register:
// WDTp: IE1, WDTCTL
// TIMERA0: TA0CTL, TA0CCTL1
// Ports: PxSEL, PxSEL2
uint8_t contextSaveSR;
uint8_t contextSaveIE1;
uint16_t contextSaveWDTCTL;
uint16_t contextSaveTA0CTL,contextSaveTA0CCTL1,contextSaveTA0CCR1;
uint8_t contextSaveSel,contextSaveSel2;
contextSaveSR = __get_SR_register();