Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z revert by Axeda Corp

Committer:
AxedaCorp
Date:
Wed Jul 02 15:59:38 2014 +0000
Revision:
1:5ad12c581db4
Parent:
0:65004368569c
url ip switch
;

Who changed what in which revision?

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