Fork of mbed_official/TSI, giving access to raw values

Dependents:   PiezoClick USB_Handson Midiudelator XtrinsicSensorEVK ... more

Fork of TSI by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TSISensor.cpp Source File

TSISensor.cpp

00001 /* Freescale Semiconductor Inc.
00002  * (c) Copyright 2004-2005 Freescale Semiconductor, Inc.
00003  * (c) Copyright 2001-2004 Motorola, Inc. 
00004  *
00005  * mbed Microcontroller Library
00006  * (c) Copyright 2009-2012 ARM Limited.
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00009  * and associated documentation files (the "Software"), to deal in the Software without
00010  * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00011  * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00012  * Software is furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in all copies or
00015  * substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00018  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00020  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022  */
00023 
00024 #include "mbed.h"
00025 #include "TSISensor.h"
00026 
00027 #define NO_TOUCH                 0
00028 #define SLIDER_LENGTH           40 //LENGTH in mm
00029 #define TOTAL_ELECTRODE          3
00030 
00031 #define TSI0a        0
00032 #define TSI1         1
00033 #define TSI2         2
00034 #define TSI3         3
00035 #define TSI4         4
00036 #define TSI5         5
00037 #define TSI6         6
00038 #define TSI7         7
00039 #define TSI8         8
00040 #define TSI9         9
00041 #define TSI10        10
00042 #define TSI11        11
00043 #define TSI12        12
00044 #define TSI13        13
00045 #define TSI14        14
00046 #define TSI15        15
00047 
00048 /*Chose the correct TSI channel for the electrode number*/
00049 #define ELECTRODE0   TSI9
00050 #define ELECTRODE1   TSI10
00051 #define ELECTRODE2   TSI0a
00052 #define ELECTRODE3   TSI1
00053 #define ELECTRODE4   TSI2
00054 #define ELECTRODE5   TSI3
00055 #define ELECTRODE6   TSI4
00056 #define ELECTRODE7   TSI5
00057 #define ELECTRODE8   TSI6
00058 #define ELECTRODE9   TSI7
00059 #define ELECTRODE10  TSI8
00060 #define ELECTRODE11  TSI11
00061 #define ELECTRODE12  TSI12
00062 #define ELECTRODE13  TSI13
00063 #define ELECTRODE14  TSI14
00064 #define ELECTRODE15  TSI15
00065 
00066 #define THRESHOLD0   100
00067 #define THRESHOLD1   100
00068 #define THRESHOLD2   100
00069 #define THRESHOLD3   100
00070 #define THRESHOLD4   100
00071 #define THRESHOLD5   100
00072 #define THRESHOLD6   100
00073 #define THRESHOLD7   100
00074 #define THRESHOLD8   100
00075 #define THRESHOLD9   100
00076 #define THRESHOLD10   100
00077 #define THRESHOLD11   100
00078 #define THRESHOLD12   100
00079 #define THRESHOLD13   100
00080 #define THRESHOLD14   100
00081 #define THRESHOLD15   100
00082 
00083 static uint8_t total_electrode = TOTAL_ELECTRODE;
00084 static uint8_t elec_array[16]={ELECTRODE0,ELECTRODE1,ELECTRODE2,ELECTRODE3,ELECTRODE4,ELECTRODE5,
00085                                ELECTRODE6,ELECTRODE7,ELECTRODE8,ELECTRODE9,ELECTRODE10,ELECTRODE11,
00086                                ELECTRODE12,ELECTRODE13,ELECTRODE14,ELECTRODE15};
00087 static uint16_t gu16TSICount[16];
00088 static uint16_t gu16Baseline[16];
00089 static uint16_t gu16Threshold[16]={THRESHOLD0,THRESHOLD1,THRESHOLD2,THRESHOLD3,THRESHOLD4,THRESHOLD5,
00090                                    THRESHOLD6,THRESHOLD7,THRESHOLD8,THRESHOLD9,THRESHOLD10,THRESHOLD11,
00091                                    THRESHOLD12,THRESHOLD13,THRESHOLD14,THRESHOLD15};
00092 static uint16_t gu16Delta[16];
00093 static uint8_t ongoing_elec;
00094 static uint8_t end_flag = 1;
00095 
00096 static uint8_t SliderPercentegePosition[2] = {NO_TOUCH,NO_TOUCH};
00097 static uint8_t SliderDistancePosition[2] = {NO_TOUCH,NO_TOUCH};
00098 static uint32_t AbsolutePercentegePosition = NO_TOUCH;
00099 static uint32_t AbsoluteDistancePosition = NO_TOUCH;
00100 
00101 static void tsi_irq();
00102 
00103 TSISensor::TSISensor() {
00104     SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
00105     SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
00106 
00107     TSI0->GENCS |= (TSI_GENCS_ESOR_MASK
00108                    | TSI_GENCS_MODE(0)
00109                    | TSI_GENCS_REFCHRG(4)
00110                    | TSI_GENCS_DVOLT(0)
00111                    | TSI_GENCS_EXTCHRG(7)
00112                    | TSI_GENCS_PS(4)
00113                    | TSI_GENCS_NSCN(11)
00114                    | TSI_GENCS_TSIIEN_MASK
00115                    | TSI_GENCS_STPE_MASK
00116                    );
00117 
00118     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
00119 
00120     NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
00121     NVIC_EnableIRQ(TSI0_IRQn);
00122 
00123     selfCalibration();
00124 }
00125 
00126 void TSISensor::TSISensor_reset(void) {
00127     SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
00128     SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
00129 
00130     TSI0->GENCS |= (TSI_GENCS_ESOR_MASK
00131                    | TSI_GENCS_MODE(0)
00132                    | TSI_GENCS_REFCHRG(4)
00133                    | TSI_GENCS_DVOLT(0)
00134                    | TSI_GENCS_EXTCHRG(7)
00135                    | TSI_GENCS_PS(4)
00136                    | TSI_GENCS_NSCN(11)
00137                    | TSI_GENCS_TSIIEN_MASK
00138                    | TSI_GENCS_STPE_MASK
00139                    );
00140 
00141     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
00142 
00143     //NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
00144     //NVIC_EnableIRQ(TSI0_IRQn);
00145 
00146     selfCalibration();
00147 }
00148 
00149 void TSISensor::selfCalibration(void)
00150 {
00151     unsigned char cnt;
00152     unsigned char trigger_backup;
00153 
00154     TSI0->GENCS |= TSI_GENCS_EOSF_MASK;      // Clear End of Scan Flag
00155     TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK;    // Disable TSI module
00156 
00157     if(TSI0->GENCS & TSI_GENCS_STM_MASK)     // Back-up TSI Trigger mode from Application
00158         trigger_backup = 1;
00159     else
00160         trigger_backup = 0;
00161 
00162     TSI0->GENCS &= ~TSI_GENCS_STM_MASK;      // Use SW trigger
00163     TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK;    // Enable TSI interrupts
00164 
00165     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;     // Enable TSI module
00166 
00167     for(cnt=0; cnt < total_electrode; cnt++)  // Get Counts when Electrode not pressed
00168     {
00169         TSI0->DATA = ((elec_array[cnt] << TSI_DATA_TSICH_SHIFT) );
00170         TSI0->DATA |= TSI_DATA_SWTS_MASK;
00171         while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK));
00172         TSI0->GENCS |= TSI_GENCS_EOSF_MASK;
00173         gu16Baseline[cnt] = (TSI0->DATA & TSI_DATA_TSICNT_MASK);
00174     }
00175 
00176     TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK;    // Disable TSI module
00177     TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK;     // Enale TSI interrupt
00178     if(trigger_backup)                      // Restore trigger mode
00179         TSI0->GENCS |= TSI_GENCS_STM_MASK;
00180     else
00181         TSI0->GENCS &= ~TSI_GENCS_STM_MASK;
00182 
00183     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;     // Enable TSI module
00184 
00185     TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) );
00186     TSI0->DATA |= TSI_DATA_SWTS_MASK;
00187 }
00188 
00189 void TSISensor::sliderRead(void ) {
00190     if(end_flag) {
00191         end_flag = 0;
00192         if((gu16Delta[0] > gu16Threshold[0])||(gu16Delta[1] > gu16Threshold[1])) {
00193             SliderPercentegePosition[0] = (gu16Delta[0]*100)/(gu16Delta[0]+gu16Delta[1]);
00194             SliderPercentegePosition[1] = (gu16Delta[1]*100)/(gu16Delta[0]+gu16Delta[1]);
00195             SliderDistancePosition[0] = (SliderPercentegePosition[0]* SLIDER_LENGTH)/100;
00196             SliderDistancePosition[1] = (SliderPercentegePosition[1]* SLIDER_LENGTH)/100;
00197             AbsolutePercentegePosition = ((100 - SliderPercentegePosition[0]) + SliderPercentegePosition[1])/2;
00198             AbsoluteDistancePosition = ((SLIDER_LENGTH - SliderDistancePosition[0]) + SliderDistancePosition[1])/2;
00199          } else {
00200             SliderPercentegePosition[0] = NO_TOUCH;
00201             SliderPercentegePosition[1] = NO_TOUCH;
00202             SliderDistancePosition[0] = NO_TOUCH;
00203             SliderDistancePosition[1] = NO_TOUCH;
00204             AbsolutePercentegePosition = NO_TOUCH;
00205             AbsoluteDistancePosition = NO_TOUCH;
00206          }
00207     }
00208 }
00209 
00210 float TSISensor::readPercentage() {
00211     sliderRead();
00212     return (float)AbsolutePercentegePosition/100.0;
00213 }
00214 
00215 uint8_t TSISensor::readDistance() {
00216     sliderRead();
00217     return AbsoluteDistancePosition;
00218 }
00219 
00220 uint16_t TSISensor::readValue(uint8_t index)
00221 {
00222     return gu16TSICount[index];
00223 }
00224 
00225 static void changeElectrode(void)
00226 {
00227     int16_t u16temp_delta;
00228 
00229     gu16TSICount[ongoing_elec] = (TSI0->DATA & TSI_DATA_TSICNT_MASK);          // Save Counts for current electrode
00230     u16temp_delta = gu16TSICount[ongoing_elec] - gu16Baseline[ongoing_elec];  // Obtains Counts Delta from callibration reference
00231     if(u16temp_delta < 0)
00232         gu16Delta[ongoing_elec] = 0;
00233     else
00234         gu16Delta[ongoing_elec] = u16temp_delta;
00235 
00236     //Change Electrode to Scan
00237     if(total_electrode > 1)  
00238     {
00239         if((total_electrode-1) > ongoing_elec)
00240             ongoing_elec++;
00241         else
00242             ongoing_elec = 0;
00243 
00244         TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) );
00245         TSI0->DATA |= TSI_DATA_SWTS_MASK;
00246     }
00247 }
00248 
00249 void tsi_irq(void)
00250 {
00251     end_flag = 1;
00252     TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
00253     changeElectrode();
00254 }