wu

Dependencies:   mbed-rtos mbed

Fork of CCC by kao yi

Committer:
backman
Date:
Mon Jun 30 03:37:05 2014 +0000
Revision:
17:3dac99cf2b89
Parent:
9:33b99cb45e99
wu

Who changed what in which revision?

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