shared

Dependencies:   mbed-rtos mbed

Fork of Boboobooo by kao yi

Committer:
Kruskal
Date:
Fri Oct 31 10:54:51 2014 +0000
Revision:
8:089b778962c4
:D

Who changed what in which revision?

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