TSI button support. You may custom the tsi channel of the button and button number.

Dependents:   FRDM_KL25Z_TSI_Touch

Fork of TSI by Mbed

Committer:
shaoziyang
Date:
Thu May 21 16:29:40 2015 +0000
Revision:
4:4b7c5856807e
Parent:
3:1a60ef257879
TSI button support.

Who changed what in which revision?

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