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

Fork of tsi_sensor by Martin Kojtal

Committer:
Kojto
Date:
Sat Feb 22 11:07:29 2014 +0000
Revision:
0:9331e373c138
Child:
1:8a2098a10330
the initial version of TSI sensor library. It contains TSI electrode and TSI Analog slider objects.

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 0:9331e373c138 22 #include "TSISensor.h"
Kojto 0:9331e373c138 23
Kojto 0:9331e373c138 24 void tsi_irq(void);
Kojto 0:9331e373c138 25 TSIAnalogSlider *TSIAnalogSlider::_instance;
Kojto 0:9331e373c138 26
Kojto 0:9331e373c138 27 TSIAnalogSlider::TSIAnalogSlider(TSIElectrode& elec0, TSIElectrode& elec1,
Kojto 0:9331e373c138 28 uint32_t range)
Kojto 0:9331e373c138 29 : _elec0(elec0), _elec1(elec1), _range(range) {
Kojto 0:9331e373c138 30 _instance = this;
Kojto 0:9331e373c138 31 _current_elec = &elec0;
Kojto 0:9331e373c138 32 SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
Kojto 0:9331e373c138 33
Kojto 0:9331e373c138 34 TSI0->GENCS |= (TSI_GENCS_ESOR_MASK | TSI_GENCS_MODE(0) | TSI_GENCS_REFCHRG(4)
Kojto 0:9331e373c138 35 | TSI_GENCS_DVOLT(0) | TSI_GENCS_EXTCHRG(7) | TSI_GENCS_PS(4)
Kojto 0:9331e373c138 36 | TSI_GENCS_NSCN(11) | TSI_GENCS_TSIIEN_MASK | TSI_GENCS_STPE_MASK);
Kojto 0:9331e373c138 37 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
Kojto 0:9331e373c138 38
Kojto 0:9331e373c138 39 NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
Kojto 0:9331e373c138 40 NVIC_EnableIRQ(TSI0_IRQn);
Kojto 0:9331e373c138 41
Kojto 0:9331e373c138 42 selfCalibration();
Kojto 0:9331e373c138 43 }
Kojto 0:9331e373c138 44
Kojto 0:9331e373c138 45 static void initBaseline(TSIElectrode& elec)
Kojto 0:9331e373c138 46 {
Kojto 0:9331e373c138 47 uint32_t channel0 = elec.getChannel();
Kojto 0:9331e373c138 48 TSI0->DATA = ((channel0 << TSI_DATA_TSICH_SHIFT) );
Kojto 0:9331e373c138 49 TSI0->DATA |= TSI_DATA_SWTS_MASK;
Kojto 0:9331e373c138 50 while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK));
Kojto 0:9331e373c138 51 TSI0->GENCS |= TSI_GENCS_EOSF_MASK;
Kojto 0:9331e373c138 52 elec.setBaseline(TSI0->DATA & TSI_DATA_TSICNT_MASK);
Kojto 0:9331e373c138 53 }
Kojto 0:9331e373c138 54
Kojto 0:9331e373c138 55 void TSIAnalogSlider::selfCalibration(void)
Kojto 0:9331e373c138 56 {
Kojto 0:9331e373c138 57 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
Kojto 0:9331e373c138 58 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
Kojto 0:9331e373c138 59
Kojto 0:9331e373c138 60 uint32_t trigger_backup;
Kojto 0:9331e373c138 61 if(TSI0->GENCS & TSI_GENCS_STM_MASK) { // Back-up TSI Trigger mode from Application
Kojto 0:9331e373c138 62 trigger_backup = 1;
Kojto 0:9331e373c138 63 } else {
Kojto 0:9331e373c138 64 trigger_backup = 0;
Kojto 0:9331e373c138 65 }
Kojto 0:9331e373c138 66
Kojto 0:9331e373c138 67 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger
Kojto 0:9331e373c138 68 TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts
Kojto 0:9331e373c138 69 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
Kojto 0:9331e373c138 70
Kojto 0:9331e373c138 71 initBaseline(_elec0);
Kojto 0:9331e373c138 72 initBaseline(_elec1);
Kojto 0:9331e373c138 73
Kojto 0:9331e373c138 74 TSI0->DATA = ((_elec0.getChannel() << TSI_DATA_TSICH_SHIFT));
Kojto 0:9331e373c138 75
Kojto 0:9331e373c138 76 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
Kojto 0:9331e373c138 77 TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK; // Enale TSI interrupt
Kojto 0:9331e373c138 78 if (trigger_backup) { // Restore trigger mode
Kojto 0:9331e373c138 79 TSI0->GENCS |= TSI_GENCS_STM_MASK;
Kojto 0:9331e373c138 80 } else {
Kojto 0:9331e373c138 81 TSI0->GENCS &= ~TSI_GENCS_STM_MASK;
Kojto 0:9331e373c138 82 }
Kojto 0:9331e373c138 83
Kojto 0:9331e373c138 84 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
Kojto 0:9331e373c138 85 TSI0->DATA |= TSI_DATA_SWTS_MASK;
Kojto 0:9331e373c138 86 }
Kojto 0:9331e373c138 87
Kojto 0:9331e373c138 88
Kojto 0:9331e373c138 89 void TSIAnalogSlider::sliderRead(void ) {
Kojto 0:9331e373c138 90 if (_scan_in_progress) {
Kojto 0:9331e373c138 91 _scan_in_progress = 0;
Kojto 0:9331e373c138 92 uint32_t delta0 = _elec0.getDelta();
Kojto 0:9331e373c138 93 uint32_t delta1 = _elec1.getDelta();
Kojto 0:9331e373c138 94
Kojto 0:9331e373c138 95 if ((delta0 > _elec0.getThreshold()) || (delta1 > _elec1.getThreshold())) {
Kojto 0:9331e373c138 96 uint32_t perc_pos0 = (delta0 * 100) / (delta0 + delta1);
Kojto 0:9331e373c138 97 uint32_t perc_pos1 = (delta1 * 100) / (delta0 + delta1);
Kojto 0:9331e373c138 98 setSliderPercPosition(0, perc_pos0);
Kojto 0:9331e373c138 99 setSliderPercPosition(1, perc_pos1);
Kojto 0:9331e373c138 100 uint32_t dist_pos0 = (perc_pos0 * _range) / 100;
Kojto 0:9331e373c138 101 uint32_t dist_pos1 = (perc_pos0 * _range) / 100;
Kojto 0:9331e373c138 102 setSliderDisPosition(0, dist_pos0);
Kojto 0:9331e373c138 103 setSliderDisPosition(1, dist_pos1);
Kojto 0:9331e373c138 104
Kojto 0:9331e373c138 105 setAbsolutePosition(((100 - perc_pos0) + perc_pos1) / 2);
Kojto 0:9331e373c138 106 setAbsoluteDistance(((_range - dist_pos0) + dist_pos1) / 2);
Kojto 0:9331e373c138 107 } else {
Kojto 0:9331e373c138 108 setSliderPercPosition(0, 0);
Kojto 0:9331e373c138 109 setSliderPercPosition(1, 0);
Kojto 0:9331e373c138 110 setSliderDisPosition(0, 0);
Kojto 0:9331e373c138 111 setSliderDisPosition(1, 0);
Kojto 0:9331e373c138 112 setAbsolutePosition(0);
Kojto 0:9331e373c138 113 setAbsoluteDistance(0);
Kojto 0:9331e373c138 114 }
Kojto 0:9331e373c138 115 }
Kojto 0:9331e373c138 116 }
Kojto 0:9331e373c138 117
Kojto 0:9331e373c138 118 float TSIAnalogSlider::readPercentage() {
Kojto 0:9331e373c138 119 sliderRead();
Kojto 0:9331e373c138 120 return (float)getAbsolutePosition() / 100.0;
Kojto 0:9331e373c138 121 }
Kojto 0:9331e373c138 122
Kojto 0:9331e373c138 123 uint32_t TSIAnalogSlider::readDistance() {
Kojto 0:9331e373c138 124 sliderRead();
Kojto 0:9331e373c138 125 return getAbsoluteDistance();
Kojto 0:9331e373c138 126 }
Kojto 0:9331e373c138 127
Kojto 0:9331e373c138 128 static void changeElectrode(TSIAnalogSlider *analog_slider)
Kojto 0:9331e373c138 129 {
Kojto 0:9331e373c138 130 TSIElectrode* elec = analog_slider->getCurrentElectrode();
Kojto 0:9331e373c138 131 uint32_t signal = (TSI0->DATA & TSI_DATA_TSICNT_MASK);
Kojto 0:9331e373c138 132 elec->setSignal(signal);
Kojto 0:9331e373c138 133
Kojto 0:9331e373c138 134 TSIElectrode *next_elec = analog_slider->getNextElectrode(elec);
Kojto 0:9331e373c138 135
Kojto 0:9331e373c138 136 analog_slider->setCurrentElectrode(next_elec);
Kojto 0:9331e373c138 137 TSI0->DATA = ((next_elec->getChannel() << TSI_DATA_TSICH_SHIFT) );
Kojto 0:9331e373c138 138 TSI0->DATA |= TSI_DATA_SWTS_MASK;
Kojto 0:9331e373c138 139 }
Kojto 0:9331e373c138 140
Kojto 0:9331e373c138 141
Kojto 0:9331e373c138 142 void tsi_irq(void)
Kojto 0:9331e373c138 143 {
Kojto 0:9331e373c138 144 TSIAnalogSlider *analog_slider = TSIAnalogSlider::getInstance();
Kojto 0:9331e373c138 145 analog_slider->setScan(1);
Kojto 0:9331e373c138 146 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
Kojto 0:9331e373c138 147 changeElectrode(analog_slider);
Kojto 0:9331e373c138 148 }