]> Pileus Git - ~andy/csm213a-hw/blob - yue/TSI/TSISensor.cpp
Add sensors code from Yue
[~andy/csm213a-hw] / yue / TSI / TSISensor.cpp
1 /* Freescale Semiconductor Inc.
2  * (c) Copyright 2004-2005 Freescale Semiconductor, Inc.
3  * (c) Copyright 2001-2004 Motorola, Inc. 
4  *
5  * mbed Microcontroller Library
6  * (c) Copyright 2009-2012 ARM Limited.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
9  * and associated documentation files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or
15  * substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
18  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 #include "mbed.h"
25 #include "TSISensor.h"
26
27 #define NO_TOUCH                 0
28 #define SLIDER_LENGTH           40 //LENGTH in mm
29 #define TOTAL_ELECTRODE          3
30
31 #define TSI0a        0
32 #define TSI1         1
33 #define TSI2         2
34 #define TSI3         3
35 #define TSI4         4
36 #define TSI5         5
37 #define TSI6         6
38 #define TSI7         7
39 #define TSI8         8
40 #define TSI9         9
41 #define TSI10        10
42 #define TSI11        11
43 #define TSI12        12
44 #define TSI13        13
45 #define TSI14        14
46 #define TSI15        15
47
48 /*Chose the correct TSI channel for the electrode number*/
49 #define ELECTRODE0   TSI9
50 #define ELECTRODE1   TSI10
51 #define ELECTRODE2   TSI0a
52 #define ELECTRODE3   TSI1
53 #define ELECTRODE4   TSI2
54 #define ELECTRODE5   TSI3
55 #define ELECTRODE6   TSI4
56 #define ELECTRODE7   TSI5
57 #define ELECTRODE8   TSI6
58 #define ELECTRODE9   TSI7
59 #define ELECTRODE10  TSI8
60 #define ELECTRODE11  TSI11
61 #define ELECTRODE12  TSI12
62 #define ELECTRODE13  TSI13
63 #define ELECTRODE14  TSI14
64 #define ELECTRODE15  TSI15
65
66 #define THRESHOLD0   100
67 #define THRESHOLD1   100
68 #define THRESHOLD2   100
69 #define THRESHOLD3   100
70 #define THRESHOLD4   100
71 #define THRESHOLD5   100
72 #define THRESHOLD6   100
73 #define THRESHOLD7   100
74 #define THRESHOLD8   100
75 #define THRESHOLD9   100
76 #define THRESHOLD10   100
77 #define THRESHOLD11   100
78 #define THRESHOLD12   100
79 #define THRESHOLD13   100
80 #define THRESHOLD14   100
81 #define THRESHOLD15   100
82
83 static uint8_t total_electrode = TOTAL_ELECTRODE;
84 static uint8_t elec_array[16]={ELECTRODE0,ELECTRODE1,ELECTRODE2,ELECTRODE3,ELECTRODE4,ELECTRODE5,
85                                ELECTRODE6,ELECTRODE7,ELECTRODE8,ELECTRODE9,ELECTRODE10,ELECTRODE11,
86                                ELECTRODE12,ELECTRODE13,ELECTRODE14,ELECTRODE15};
87 static uint16_t gu16TSICount[16];
88 static uint16_t gu16Baseline[16];
89 static uint16_t gu16Threshold[16]={THRESHOLD0,THRESHOLD1,THRESHOLD2,THRESHOLD3,THRESHOLD4,THRESHOLD5,
90                                    THRESHOLD6,THRESHOLD7,THRESHOLD8,THRESHOLD9,THRESHOLD10,THRESHOLD11,
91                                    THRESHOLD12,THRESHOLD13,THRESHOLD14,THRESHOLD15};
92 static uint16_t gu16Delta[16];
93 static uint8_t ongoing_elec;
94 static uint8_t end_flag = 1;
95
96 static uint8_t SliderPercentegePosition[2] = {NO_TOUCH,NO_TOUCH};
97 static uint8_t SliderDistancePosition[2] = {NO_TOUCH,NO_TOUCH};
98 static uint32_t AbsolutePercentegePosition = NO_TOUCH;
99 static uint32_t AbsoluteDistancePosition = NO_TOUCH;
100
101 static void tsi_irq();
102
103 TSISensor::TSISensor() {
104     SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
105     SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
106
107     TSI0->GENCS |= (TSI_GENCS_ESOR_MASK
108                    | TSI_GENCS_MODE(0)
109                    | TSI_GENCS_REFCHRG(4)
110                    | TSI_GENCS_DVOLT(0)
111                    | TSI_GENCS_EXTCHRG(7)
112                    | TSI_GENCS_PS(4)
113                    | TSI_GENCS_NSCN(11)
114                    | TSI_GENCS_TSIIEN_MASK
115                    | TSI_GENCS_STPE_MASK
116                    );
117
118     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
119
120     NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
121     NVIC_EnableIRQ(TSI0_IRQn);
122
123     selfCalibration();
124 }
125
126 void TSISensor::TSISensor_reset(void) {
127     SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
128     SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
129
130     TSI0->GENCS |= (TSI_GENCS_ESOR_MASK
131                    | TSI_GENCS_MODE(0)
132                    | TSI_GENCS_REFCHRG(4)
133                    | TSI_GENCS_DVOLT(0)
134                    | TSI_GENCS_EXTCHRG(7)
135                    | TSI_GENCS_PS(4)
136                    | TSI_GENCS_NSCN(11)
137                    | TSI_GENCS_TSIIEN_MASK
138                    | TSI_GENCS_STPE_MASK
139                    );
140
141     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
142
143     //NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
144     //NVIC_EnableIRQ(TSI0_IRQn);
145
146     selfCalibration();
147 }
148
149 void TSISensor::selfCalibration(void)
150 {
151     unsigned char cnt;
152     unsigned char trigger_backup;
153
154     TSI0->GENCS |= TSI_GENCS_EOSF_MASK;      // Clear End of Scan Flag
155     TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK;    // Disable TSI module
156
157     if(TSI0->GENCS & TSI_GENCS_STM_MASK)     // Back-up TSI Trigger mode from Application
158         trigger_backup = 1;
159     else
160         trigger_backup = 0;
161
162     TSI0->GENCS &= ~TSI_GENCS_STM_MASK;      // Use SW trigger
163     TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK;    // Enable TSI interrupts
164
165     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;     // Enable TSI module
166
167     for(cnt=0; cnt < total_electrode; cnt++)  // Get Counts when Electrode not pressed
168     {
169         TSI0->DATA = ((elec_array[cnt] << TSI_DATA_TSICH_SHIFT) );
170         TSI0->DATA |= TSI_DATA_SWTS_MASK;
171         while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK));
172         TSI0->GENCS |= TSI_GENCS_EOSF_MASK;
173         gu16Baseline[cnt] = (TSI0->DATA & TSI_DATA_TSICNT_MASK);
174     }
175
176     TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK;    // Disable TSI module
177     TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK;     // Enale TSI interrupt
178     if(trigger_backup)                      // Restore trigger mode
179         TSI0->GENCS |= TSI_GENCS_STM_MASK;
180     else
181         TSI0->GENCS &= ~TSI_GENCS_STM_MASK;
182
183     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;     // Enable TSI module
184
185     TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) );
186     TSI0->DATA |= TSI_DATA_SWTS_MASK;
187 }
188
189 void TSISensor::sliderRead(void ) {
190     if(end_flag) {
191         end_flag = 0;
192         if((gu16Delta[0] > gu16Threshold[0])||(gu16Delta[1] > gu16Threshold[1])) {
193             SliderPercentegePosition[0] = (gu16Delta[0]*100)/(gu16Delta[0]+gu16Delta[1]);
194             SliderPercentegePosition[1] = (gu16Delta[1]*100)/(gu16Delta[0]+gu16Delta[1]);
195             SliderDistancePosition[0] = (SliderPercentegePosition[0]* SLIDER_LENGTH)/100;
196             SliderDistancePosition[1] = (SliderPercentegePosition[1]* SLIDER_LENGTH)/100;
197             AbsolutePercentegePosition = ((100 - SliderPercentegePosition[0]) + SliderPercentegePosition[1])/2;
198             AbsoluteDistancePosition = ((SLIDER_LENGTH - SliderDistancePosition[0]) + SliderDistancePosition[1])/2;
199          } else {
200             SliderPercentegePosition[0] = NO_TOUCH;
201             SliderPercentegePosition[1] = NO_TOUCH;
202             SliderDistancePosition[0] = NO_TOUCH;
203             SliderDistancePosition[1] = NO_TOUCH;
204             AbsolutePercentegePosition = NO_TOUCH;
205             AbsoluteDistancePosition = NO_TOUCH;
206          }
207     }
208 }
209
210 float TSISensor::readPercentage() {
211     sliderRead();
212     return (float)AbsolutePercentegePosition/100.0;
213 }
214
215 uint8_t TSISensor::readDistance() {
216     sliderRead();
217     return AbsoluteDistancePosition;
218 }
219
220 uint16_t TSISensor::readValue(uint8_t index)
221 {
222     return gu16TSICount[index];
223 }
224
225 static void changeElectrode(void)
226 {
227     int16_t u16temp_delta;
228
229     gu16TSICount[ongoing_elec] = (TSI0->DATA & TSI_DATA_TSICNT_MASK);          // Save Counts for current electrode
230     u16temp_delta = gu16TSICount[ongoing_elec] - gu16Baseline[ongoing_elec];  // Obtains Counts Delta from callibration reference
231     if(u16temp_delta < 0)
232         gu16Delta[ongoing_elec] = 0;
233     else
234         gu16Delta[ongoing_elec] = u16temp_delta;
235
236     //Change Electrode to Scan
237     if(total_electrode > 1)  
238     {
239         if((total_electrode-1) > ongoing_elec)
240             ongoing_elec++;
241         else
242             ongoing_elec = 0;
243
244         TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) );
245         TSI0->DATA |= TSI_DATA_SWTS_MASK;
246     }
247 }
248
249 void tsi_irq(void)
250 {
251     end_flag = 1;
252     TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
253     changeElectrode();
254 }