TSISensor.cpp 7.8 KB

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