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.
25 #include "TSISensor.h"
28 #define SLIDER_LENGTH 40 //LENGTH in mm
29 #define TOTAL_ELECTRODE 3
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);
126 void TSISensor::TSISensor_reset(void) {
127 SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
128 SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
130 TSI0->GENCS |= (TSI_GENCS_ESOR_MASK
132 | TSI_GENCS_REFCHRG(4)
134 | TSI_GENCS_EXTCHRG(7)
137 | TSI_GENCS_TSIIEN_MASK
138 | TSI_GENCS_STPE_MASK
141 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
143 //NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
144 //NVIC_EnableIRQ(TSI0_IRQn);
149 void TSISensor::selfCalibration(void)
152 unsigned char trigger_backup;
154 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
155 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
157 if(TSI0->GENCS & TSI_GENCS_STM_MASK) // Back-up TSI Trigger mode from Application
162 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger
163 TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts
165 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
167 for(cnt=0; cnt < total_electrode; cnt++) // Get Counts when Electrode not pressed
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);
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;
181 TSI0->GENCS &= ~TSI_GENCS_STM_MASK;
183 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
185 TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) );
186 TSI0->DATA |= TSI_DATA_SWTS_MASK;
189 void TSISensor::sliderRead(void ) {
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;
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;
210 float TSISensor::readPercentage() {
212 return (float)AbsolutePercentegePosition/100.0;
215 uint8_t TSISensor::readDistance() {
217 return AbsoluteDistancePosition;
220 uint16_t TSISensor::readValue(uint8_t index)
222 return gu16TSICount[index];
225 static void changeElectrode(void)
227 int16_t u16temp_delta;
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;
234 gu16Delta[ongoing_elec] = u16temp_delta;
236 //Change Electrode to Scan
237 if(total_electrode > 1)
239 if((total_electrode-1) > ongoing_elec)
244 TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) );
245 TSI0->DATA |= TSI_DATA_SWTS_MASK;
252 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag