Fork of tsi_sensor library that allows object construction with pin names instead of TSI channels

Fork of tsi_sensor by Martin Kojtal

Committer:
bjo3rn
Date:
Wed Aug 06 08:33:46 2014 +0000
Revision:
5:6b2d75d82058
Parent:
4:f64097679f27
Added KL46Z pin->channel mapping; converted compile time error for other platforms to runtime error (so error only occurs if code is actually called, not for programs that don't use the pin map)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:9331e373c138 1 /* Freescale Semiconductor Inc.
Kojto 0:9331e373c138 2 * mbed Microcontroller Library
Kojto 0:9331e373c138 3 * (c) Copyright 2014 ARM Limited.
Kojto 0:9331e373c138 4 *
Kojto 0:9331e373c138 5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Kojto 0:9331e373c138 6 * and associated documentation files (the "Software"), to deal in the Software without
Kojto 0:9331e373c138 7 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Kojto 0:9331e373c138 8 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Kojto 0:9331e373c138 9 * Software is furnished to do so, subject to the following conditions:
Kojto 0:9331e373c138 10 *
Kojto 0:9331e373c138 11 * The above copyright notice and this permission notice shall be included in all copies or
Kojto 0:9331e373c138 12 * substantial portions of the Software.
Kojto 0:9331e373c138 13 *
Kojto 0:9331e373c138 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Kojto 0:9331e373c138 15 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Kojto 0:9331e373c138 16 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Kojto 0:9331e373c138 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Kojto 0:9331e373c138 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kojto 0:9331e373c138 19 */
Kojto 0:9331e373c138 20
Kojto 0:9331e373c138 21 #include "mbed.h"
Kojto 1:8a2098a10330 22 #include "tsi_sensor.h"
Kojto 0:9331e373c138 23
Kojto 0:9331e373c138 24 void tsi_irq(void);
Kojto 0:9331e373c138 25 TSIAnalogSlider *TSIAnalogSlider::_instance;
Kojto 0:9331e373c138 26
bjo3rn 4:f64097679f27 27 TSIAnalogSlider::TSIAnalogSlider(PinName pin0, PinName pin1, uint32_t range): _elec0(pin0), _elec1(pin1), _range(range) {
bjo3rn 4:f64097679f27 28 initObject();
bjo3rn 4:f64097679f27 29 }
Kojto 3:20ffa9b18488 30 TSIAnalogSlider::TSIAnalogSlider(uint32_t elec0, uint32_t elec1,
Kojto 0:9331e373c138 31 uint32_t range)
Kojto 0:9331e373c138 32 : _elec0(elec0), _elec1(elec1), _range(range) {
bjo3rn 4:f64097679f27 33 initObject();
bjo3rn 4:f64097679f27 34 }
bjo3rn 4:f64097679f27 35
bjo3rn 4:f64097679f27 36 void TSIAnalogSlider::initObject(void) {
Kojto 0:9331e373c138 37 _instance = this;
Kojto 3:20ffa9b18488 38 _current_elec = &_elec0;
Kojto 0:9331e373c138 39 SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
Kojto 0:9331e373c138 40
Kojto 0:9331e373c138 41 TSI0->GENCS |= (TSI_GENCS_ESOR_MASK | TSI_GENCS_MODE(0) | TSI_GENCS_REFCHRG(4)
Kojto 0:9331e373c138 42 | TSI_GENCS_DVOLT(0) | TSI_GENCS_EXTCHRG(7) | TSI_GENCS_PS(4)
Kojto 0:9331e373c138 43 | TSI_GENCS_NSCN(11) | TSI_GENCS_TSIIEN_MASK | TSI_GENCS_STPE_MASK);
Kojto 0:9331e373c138 44 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
Kojto 0:9331e373c138 45
Kojto 0:9331e373c138 46 NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
Kojto 0:9331e373c138 47 NVIC_EnableIRQ(TSI0_IRQn);
Kojto 0:9331e373c138 48
Kojto 0:9331e373c138 49 selfCalibration();
Kojto 0:9331e373c138 50 }
Kojto 0:9331e373c138 51
Kojto 0:9331e373c138 52 static void initBaseline(TSIElectrode& elec)
Kojto 0:9331e373c138 53 {
Kojto 0:9331e373c138 54 uint32_t channel0 = elec.getChannel();
Kojto 0:9331e373c138 55 TSI0->DATA = ((channel0 << TSI_DATA_TSICH_SHIFT) );
Kojto 0:9331e373c138 56 TSI0->DATA |= TSI_DATA_SWTS_MASK;
Kojto 0:9331e373c138 57 while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK));
Kojto 0:9331e373c138 58 TSI0->GENCS |= TSI_GENCS_EOSF_MASK;
Kojto 0:9331e373c138 59 elec.setBaseline(TSI0->DATA & TSI_DATA_TSICNT_MASK);
Kojto 0:9331e373c138 60 }
Kojto 0:9331e373c138 61
Kojto 0:9331e373c138 62 void TSIAnalogSlider::selfCalibration(void)
Kojto 0:9331e373c138 63 {
Kojto 0:9331e373c138 64 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
Kojto 0:9331e373c138 65 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
Kojto 0:9331e373c138 66
Kojto 0:9331e373c138 67 uint32_t trigger_backup;
Kojto 0:9331e373c138 68 if(TSI0->GENCS & TSI_GENCS_STM_MASK) { // Back-up TSI Trigger mode from Application
Kojto 0:9331e373c138 69 trigger_backup = 1;
Kojto 0:9331e373c138 70 } else {
Kojto 0:9331e373c138 71 trigger_backup = 0;
Kojto 0:9331e373c138 72 }
Kojto 0:9331e373c138 73
Kojto 0:9331e373c138 74 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger
Kojto 0:9331e373c138 75 TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts
Kojto 0:9331e373c138 76 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
Kojto 0:9331e373c138 77
Kojto 0:9331e373c138 78 initBaseline(_elec0);
Kojto 0:9331e373c138 79 initBaseline(_elec1);
Kojto 0:9331e373c138 80
Kojto 0:9331e373c138 81 TSI0->DATA = ((_elec0.getChannel() << TSI_DATA_TSICH_SHIFT));
Kojto 0:9331e373c138 82
Kojto 0:9331e373c138 83 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
Kojto 0:9331e373c138 84 TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK; // Enale TSI interrupt
Kojto 0:9331e373c138 85 if (trigger_backup) { // Restore trigger mode
Kojto 0:9331e373c138 86 TSI0->GENCS |= TSI_GENCS_STM_MASK;
Kojto 0:9331e373c138 87 } else {
Kojto 0:9331e373c138 88 TSI0->GENCS &= ~TSI_GENCS_STM_MASK;
Kojto 0:9331e373c138 89 }
Kojto 0:9331e373c138 90
Kojto 0:9331e373c138 91 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
Kojto 0:9331e373c138 92 TSI0->DATA |= TSI_DATA_SWTS_MASK;
Kojto 0:9331e373c138 93 }
Kojto 0:9331e373c138 94
Kojto 0:9331e373c138 95
Kojto 0:9331e373c138 96 void TSIAnalogSlider::sliderRead(void ) {
Kojto 0:9331e373c138 97 if (_scan_in_progress) {
Kojto 0:9331e373c138 98 _scan_in_progress = 0;
Kojto 0:9331e373c138 99 uint32_t delta0 = _elec0.getDelta();
Kojto 0:9331e373c138 100 uint32_t delta1 = _elec1.getDelta();
Kojto 0:9331e373c138 101
Kojto 0:9331e373c138 102 if ((delta0 > _elec0.getThreshold()) || (delta1 > _elec1.getThreshold())) {
Kojto 0:9331e373c138 103 uint32_t perc_pos0 = (delta0 * 100) / (delta0 + delta1);
Kojto 0:9331e373c138 104 uint32_t perc_pos1 = (delta1 * 100) / (delta0 + delta1);
Kojto 0:9331e373c138 105 setSliderPercPosition(0, perc_pos0);
Kojto 0:9331e373c138 106 setSliderPercPosition(1, perc_pos1);
Kojto 0:9331e373c138 107 uint32_t dist_pos0 = (perc_pos0 * _range) / 100;
Kojto 0:9331e373c138 108 uint32_t dist_pos1 = (perc_pos0 * _range) / 100;
Kojto 0:9331e373c138 109 setSliderDisPosition(0, dist_pos0);
Kojto 0:9331e373c138 110 setSliderDisPosition(1, dist_pos1);
Kojto 0:9331e373c138 111
Kojto 0:9331e373c138 112 setAbsolutePosition(((100 - perc_pos0) + perc_pos1) / 2);
Kojto 0:9331e373c138 113 setAbsoluteDistance(((_range - dist_pos0) + dist_pos1) / 2);
Kojto 0:9331e373c138 114 } else {
Kojto 0:9331e373c138 115 setSliderPercPosition(0, 0);
Kojto 0:9331e373c138 116 setSliderPercPosition(1, 0);
Kojto 0:9331e373c138 117 setSliderDisPosition(0, 0);
Kojto 0:9331e373c138 118 setSliderDisPosition(1, 0);
Kojto 0:9331e373c138 119 setAbsolutePosition(0);
Kojto 0:9331e373c138 120 setAbsoluteDistance(0);
Kojto 0:9331e373c138 121 }
Kojto 0:9331e373c138 122 }
Kojto 0:9331e373c138 123 }
Kojto 0:9331e373c138 124
Kojto 0:9331e373c138 125 float TSIAnalogSlider::readPercentage() {
Kojto 0:9331e373c138 126 sliderRead();
Kojto 0:9331e373c138 127 return (float)getAbsolutePosition() / 100.0;
Kojto 0:9331e373c138 128 }
Kojto 0:9331e373c138 129
Kojto 0:9331e373c138 130 uint32_t TSIAnalogSlider::readDistance() {
Kojto 0:9331e373c138 131 sliderRead();
Kojto 0:9331e373c138 132 return getAbsoluteDistance();
Kojto 0:9331e373c138 133 }
Kojto 0:9331e373c138 134
Kojto 0:9331e373c138 135 static void changeElectrode(TSIAnalogSlider *analog_slider)
Kojto 0:9331e373c138 136 {
Kojto 0:9331e373c138 137 TSIElectrode* elec = analog_slider->getCurrentElectrode();
Kojto 0:9331e373c138 138 uint32_t signal = (TSI0->DATA & TSI_DATA_TSICNT_MASK);
Kojto 0:9331e373c138 139 elec->setSignal(signal);
Kojto 0:9331e373c138 140
Kojto 0:9331e373c138 141 TSIElectrode *next_elec = analog_slider->getNextElectrode(elec);
Kojto 0:9331e373c138 142
Kojto 0:9331e373c138 143 analog_slider->setCurrentElectrode(next_elec);
Kojto 0:9331e373c138 144 TSI0->DATA = ((next_elec->getChannel() << TSI_DATA_TSICH_SHIFT) );
Kojto 0:9331e373c138 145 TSI0->DATA |= TSI_DATA_SWTS_MASK;
Kojto 0:9331e373c138 146 }
Kojto 0:9331e373c138 147
Kojto 0:9331e373c138 148
Kojto 0:9331e373c138 149 void tsi_irq(void)
Kojto 0:9331e373c138 150 {
Kojto 0:9331e373c138 151 TSIAnalogSlider *analog_slider = TSIAnalogSlider::getInstance();
Kojto 0:9331e373c138 152 analog_slider->setScan(1);
Kojto 0:9331e373c138 153 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
Kojto 0:9331e373c138 154 changeElectrode(analog_slider);
Kojto 0:9331e373c138 155 }