]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/tests/peripherals/TSI/TSISensor.cpp
Merge commit '657d9f23fe47fb88cf221adb23095082f191ba6a'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / tests / peripherals / 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 2
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
127 void TSISensor::selfCalibration(void)
128 {
129 unsigned char cnt;
130 unsigned char trigger_backup;
131
132 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
133 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
134
135 if(TSI0->GENCS & TSI_GENCS_STM_MASK) // Back-up TSI Trigger mode from Application
136 trigger_backup = 1;
137 else
138 trigger_backup = 0;
139
140 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger
141 TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts
142
143 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
144
145 for(cnt=0; cnt < total_electrode; cnt++) // Get Counts when Electrode not pressed
146 {
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);
152 }
153
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;
158 else
159 TSI0->GENCS &= ~TSI_GENCS_STM_MASK;
160
161 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
162
163 TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) );
164 TSI0->DATA |= TSI_DATA_SWTS_MASK;
165 }
166
167
168 void TSISensor::sliderRead(void ) {
169 if(end_flag) {
170 end_flag = 0;
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;
178 } else {
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;
185 }
186 }
187 }
188
189 float TSISensor::readPercentage() {
190 sliderRead();
191 return (float)AbsolutePercentegePosition/100.0;
192 }
193
194 uint8_t TSISensor::readDistance() {
195 sliderRead();
196 return AbsoluteDistancePosition;
197 }
198
199
200 static void changeElectrode(void)
201 {
202 int16_t u16temp_delta;
203
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;
208 else
209 gu16Delta[ongoing_elec] = u16temp_delta;
210
211 //Change Electrode to Scan
212 if(total_electrode > 1)
213 {
214 if((total_electrode-1) > ongoing_elec)
215 ongoing_elec++;
216 else
217 ongoing_elec = 0;
218
219 TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) );
220 TSI0->DATA |= TSI_DATA_SWTS_MASK;
221 }
222 }
223
224
225 void tsi_irq(void)
226 {
227 end_flag = 1;
228 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
229 changeElectrode();
230 }
231
Imprint / Impressum