Generate the maximum sampling rate

Dependencies:   MAG31101 mbed

Committer:
mja054
Date:
Sat Feb 15 01:07:15 2014 +0000
Revision:
1:478b36058c92
Parent:
0:d62b58e30f71
Generate the maximum sampling rate

Who changed what in which revision?

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