1 /* Freescale Semiconductor Inc.
2 * (c) Copyright 2004-2005 Freescale Semiconductor, Inc.
3 * (c) Copyright 2001-2004 Motorola, Inc.
5 * mbed Microcontroller Library
6 * (c) Copyright 2009-2012 ARM Limited.
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:
14 * The above copyright notice and this permission notice shall be included in all copies or
15 * substantial portions of the Software.
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.
28 #define SLIDER_LENGTH 40 //LENGTH in mm
29 #define TOTAL_ELECTRODE 2
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
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
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;
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;
101 static void tsi_irq();
103 TSISensor::TSISensor() {
104 SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
105 SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
107 TSI0->GENCS |= (TSI_GENCS_ESOR_MASK
109 | TSI_GENCS_REFCHRG(4)
111 | TSI_GENCS_EXTCHRG(7)
114 | TSI_GENCS_TSIIEN_MASK
115 | TSI_GENCS_STPE_MASK
118 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
120 NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
121 NVIC_EnableIRQ(TSI0_IRQn);
127 void TSISensor::selfCalibration(void)
130 unsigned char trigger_backup;
132 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
133 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
135 if(TSI0->GENCS & TSI_GENCS_STM_MASK) // Back-up TSI Trigger mode from Application
140 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger
141 TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts
143 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
145 for(cnt=0; cnt < total_electrode; cnt++) // Get Counts when Electrode not pressed
147 TSI0->DATA = ((elec_array[cnt] << TSI_DATA_TSICH_SHIFT) );
148 TSI0->DATA |= TSI_DATA_SWTS_MASK;
149 while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK));
150 TSI0->GENCS |= TSI_GENCS_EOSF_MASK;
151 gu16Baseline[cnt] = (TSI0->DATA & TSI_DATA_TSICNT_MASK);
154 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
155 TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK; // Enale TSI interrupt
156 if(trigger_backup) // Restore trigger mode
157 TSI0->GENCS |= TSI_GENCS_STM_MASK;
159 TSI0->GENCS &= ~TSI_GENCS_STM_MASK;
161 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
163 TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) );
164 TSI0->DATA |= TSI_DATA_SWTS_MASK;
168 void TSISensor::sliderRead(void ) {
171 if((gu16Delta[0] > gu16Threshold[0])||(gu16Delta[1] > gu16Threshold[1])) {
172 SliderPercentegePosition[0] = (gu16Delta[0]*100)/(gu16Delta[0]+gu16Delta[1]);
173 SliderPercentegePosition[1] = (gu16Delta[1]*100)/(gu16Delta[0]+gu16Delta[1]);
174 SliderDistancePosition[0] = (SliderPercentegePosition[0]* SLIDER_LENGTH)/100;
175 SliderDistancePosition[1] = (SliderPercentegePosition[1]* SLIDER_LENGTH)/100;
176 AbsolutePercentegePosition = ((100 - SliderPercentegePosition[0]) + SliderPercentegePosition[1])/2;
177 AbsoluteDistancePosition = ((SLIDER_LENGTH - SliderDistancePosition[0]) + SliderDistancePosition[1])/2;
179 SliderPercentegePosition[0] = NO_TOUCH;
180 SliderPercentegePosition[1] = NO_TOUCH;
181 SliderDistancePosition[0] = NO_TOUCH;
182 SliderDistancePosition[1] = NO_TOUCH;
183 AbsolutePercentegePosition = NO_TOUCH;
184 AbsoluteDistancePosition = NO_TOUCH;
189 float TSISensor::readPercentage() {
191 return (float)AbsolutePercentegePosition/100.0;
194 uint8_t TSISensor::readDistance() {
196 return AbsoluteDistancePosition;
200 static void changeElectrode(void)
202 int16_t u16temp_delta;
204 gu16TSICount[ongoing_elec] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); // Save Counts for current electrode
205 u16temp_delta = gu16TSICount[ongoing_elec] - gu16Baseline[ongoing_elec]; // Obtains Counts Delta from callibration reference
206 if(u16temp_delta < 0)
207 gu16Delta[ongoing_elec] = 0;
209 gu16Delta[ongoing_elec] = u16temp_delta;
211 //Change Electrode to Scan
212 if(total_electrode > 1)
214 if((total_electrode-1) > ongoing_elec)
219 TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) );
220 TSI0->DATA |= TSI_DATA_SWTS_MASK;
228 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag