xypad theremin for LPC1768

Dependencies:   MODDMA mbed

Committer:
exopiped
Date:
Mon Mar 14 23:43:44 2016 +0000
Revision:
2:c5eeaf1c8e69
Parent:
1:aa184d2eb2e3
touch screen driver glitches removed; debug statements commented out

Who changed what in which revision?

UserRevisionLine numberNew contents of line
exopiped 0:8ee38453bad9 1 /*
exopiped 0:8ee38453bad9 2 * touch.cpp -- touch screen monitor
exopiped 0:8ee38453bad9 3 */
exopiped 0:8ee38453bad9 4 #include "mbed.h"
exopiped 0:8ee38453bad9 5 #include "debug.h"
exopiped 0:8ee38453bad9 6 #include "touch.h"
exopiped 0:8ee38453bad9 7
exopiped 0:8ee38453bad9 8 #define NIL (-1)
exopiped 0:8ee38453bad9 9 /*
exopiped 0:8ee38453bad9 10 * Connecting the touch screen
exopiped 0:8ee38453bad9 11 * STMPE610 MODE pin is tied low for I2C interface
exopiped 0:8ee38453bad9 12 * STMPE610 A0 pin tied low for I2C address 0x41
exopiped 0:8ee38453bad9 13 * (0x82 when shifted left 1)
exopiped 0:8ee38453bad9 14 * STMPE610 A0 pin tied high for I2C address 0x44
exopiped 0:8ee38453bad9 15 * (0x88 when shifted left 1)
exopiped 0:8ee38453bad9 16 *
exopiped 0:8ee38453bad9 17 * I2C works on board pins 9 and 10. (SDA, SDL)
exopiped 0:8ee38453bad9 18 * ( STMPE610 pins SDAT and SCLK )
exopiped 0:8ee38453bad9 19 *
exopiped 0:8ee38453bad9 20 * Setting up the touch screen
exopiped 0:8ee38453bad9 21 *
exopiped 0:8ee38453bad9 22 * Disable Touch Screen and A/D clock -- SYS_CTRL2
exopiped 0:8ee38453bad9 23 * Set TSC_OFF bit high, ADC_OFF bit high
exopiped 0:8ee38453bad9 24 *
exopiped 0:8ee38453bad9 25 * ConfigureTouch Screen -- TSCFG register
exopiped 0:8ee38453bad9 26 * Set up for 4 sample averaging
exopiped 0:8ee38453bad9 27 * Touch detect delay of 1 msec
exopiped 0:8ee38453bad9 28 * Settling time of 1 msec
exopiped 0:8ee38453bad9 29 *
exopiped 0:8ee38453bad9 30 * Touchscreen Window -- WDW_TR_X, WDW_TR_Y, WDW_BL_X, WDW_BLY
exopiped 0:8ee38453bad9 31 * Set up for full screen ( default condition )
exopiped 0:8ee38453bad9 32 *
exopiped 0:8ee38453bad9 33 */
exopiped 0:8ee38453bad9 34
exopiped 1:aa184d2eb2e3 35 typedef enum {TOUCH_NOTOUCH,TOUCH_DEBOUNCE,NOTOUCH_DEBOUNCE,TOUCH_PRESENT} TOUCH_STATE;
exopiped 0:8ee38453bad9 36
exopiped 0:8ee38453bad9 37
exopiped 0:8ee38453bad9 38 #define TSC_ADDR 0x82 // (i2c address for touchscreen)<<1
exopiped 0:8ee38453bad9 39
exopiped 0:8ee38453bad9 40 // CHIP_ID register
exopiped 0:8ee38453bad9 41 #define CHIP_ID 0x00 // 16 bit register
exopiped 0:8ee38453bad9 42
exopiped 0:8ee38453bad9 43 // ID_VER register
exopiped 0:8ee38453bad9 44 #define ID_VER 0x02 // 8 bit register
exopiped 0:8ee38453bad9 45
exopiped 0:8ee38453bad9 46 // SYS_CTRL1 Reset control register
exopiped 0:8ee38453bad9 47 #define SYS_CTRL1 0x03
exopiped 0:8ee38453bad9 48 #define SOFT_RESET 0x02
exopiped 0:8ee38453bad9 49
exopiped 0:8ee38453bad9 50 // SYS_CTRL2 clock control register
exopiped 0:8ee38453bad9 51 #define SYS_CTRL2 0x04
exopiped 0:8ee38453bad9 52 #define ADC_OFF 0x01
exopiped 0:8ee38453bad9 53 #define TSC_OFF 0x02
exopiped 0:8ee38453bad9 54 #define GPIO_OFF 0x04
exopiped 0:8ee38453bad9 55
exopiped 0:8ee38453bad9 56 // ADC_CTRL registers
exopiped 0:8ee38453bad9 57 #define ADC_CTRL1 0x20
exopiped 0:8ee38453bad9 58 #define ADC_CTRL2 0x21
exopiped 0:8ee38453bad9 59
exopiped 0:8ee38453bad9 60 // Interrupt control, enable and status registers
exopiped 0:8ee38453bad9 61 #define INT_CTRL 0x09
exopiped 0:8ee38453bad9 62 #define INT_EN 0x0A
exopiped 0:8ee38453bad9 63 #define INT_STA 0x0B
exopiped 0:8ee38453bad9 64
exopiped 0:8ee38453bad9 65 // TSC_CTRL touchscreen control register
exopiped 0:8ee38453bad9 66 #define TSC_CTRL 0x40
exopiped 0:8ee38453bad9 67 #define TSC_TOUCH_DET 0x80 // 1 when touch detected else 0
exopiped 0:8ee38453bad9 68 #define TSC_TRACK_MASK 0x70 // 0 => no window tracking
exopiped 0:8ee38453bad9 69 #define TSC_OP_MOD_MASK 0x0E // 0 => XYZ acquisition
exopiped 0:8ee38453bad9 70 #define TSC_EN_MASK 0x01 // enable touchscreen
exopiped 0:8ee38453bad9 71
exopiped 0:8ee38453bad9 72 // TSC_CFG touchscreen config register
exopiped 0:8ee38453bad9 73 #define TSC_CFG 0x41
exopiped 0:8ee38453bad9 74 #define TSC_AVG4 0x80
exopiped 0:8ee38453bad9 75 #define TSC_DLY_1MS 0x20
exopiped 0:8ee38453bad9 76 #define TSC_STL_1MS 0x03
exopiped 0:8ee38453bad9 77
exopiped 0:8ee38453bad9 78 #define TSC_I_DRIVE 0x58
exopiped 0:8ee38453bad9 79 #define MAMP_50 0x01
exopiped 0:8ee38453bad9 80
exopiped 0:8ee38453bad9 81 // FIFO_TH touchscreen fifo threshold register
exopiped 0:8ee38453bad9 82 #define FIFO_TH 0x4A
exopiped 0:8ee38453bad9 83
exopiped 0:8ee38453bad9 84 // TSC_DATA_X X data register
exopiped 0:8ee38453bad9 85 #define TSC_DATA_X 0x4D
exopiped 0:8ee38453bad9 86
exopiped 0:8ee38453bad9 87 // TSC_DATA_Y Y data register
exopiped 0:8ee38453bad9 88 #define TSC_DATA_Y 0x4F
exopiped 0:8ee38453bad9 89
exopiped 0:8ee38453bad9 90 // FIFO_STA touchscreen fifo control-status register
exopiped 0:8ee38453bad9 91 #define FIFO_STA 0x4B
exopiped 0:8ee38453bad9 92 #define FIFO_OFLOW 0x80
exopiped 0:8ee38453bad9 93 #define FIFO_FULL 0x40
exopiped 0:8ee38453bad9 94 #define FIFO_EMPTY 0x20
exopiped 0:8ee38453bad9 95 #define FIFO_TRGD 0x10
exopiped 0:8ee38453bad9 96 #define FIFO_RESET 0x01
exopiped 0:8ee38453bad9 97
exopiped 0:8ee38453bad9 98 // TSC_DATA touchscreen data register
exopiped 0:8ee38453bad9 99 #define TSC_DATA 0xD7
exopiped 0:8ee38453bad9 100
exopiped 0:8ee38453bad9 101 // GPIO_AF -- GPIO Alternate FunctionRegister
exopiped 0:8ee38453bad9 102 #define GPIO_AF 0x17
exopiped 1:aa184d2eb2e3 103 DigitalOut led1(LED1);
exopiped 0:8ee38453bad9 104 DigitalOut led4(LED4);
exopiped 0:8ee38453bad9 105
exopiped 0:8ee38453bad9 106 i2c_t touch_ctrl; // i2c interface struct for touch screen
exopiped 0:8ee38453bad9 107
exopiped 1:aa184d2eb2e3 108 static int touch_present(void);
exopiped 1:aa184d2eb2e3 109 static void touch_reset(void);
exopiped 0:8ee38453bad9 110 static void touch_compute_params(void);
exopiped 0:8ee38453bad9 111
exopiped 0:8ee38453bad9 112 static char tsc_reset[2]={SYS_CTRL1, SOFT_RESET};
exopiped 0:8ee38453bad9 113 static char clox_on[2]={SYS_CTRL2,0x00};
exopiped 0:8ee38453bad9 114 static char adc_ctrl1[2]={ADC_CTRL1,0x49};
exopiped 0:8ee38453bad9 115 static char adc_ctrl2[2]={ADC_CTRL2,0x01};
exopiped 0:8ee38453bad9 116 static char gpio_af[2]={GPIO_AF,0};
exopiped 0:8ee38453bad9 117 static char fifo_clear[2]={FIFO_STA,0x01};
exopiped 0:8ee38453bad9 118 static char fifo_operate[2]={FIFO_STA,0x00};
exopiped 0:8ee38453bad9 119 static char touch_int_en[2]={INT_EN,0x02};//enable FIFO_TH int
exopiped 0:8ee38453bad9 120 static char clr_intrupts[2]={INT_STA,0xFF};
exopiped 0:8ee38453bad9 121 static char ena_intrupt[2]={INT_CTRL,0x02};
exopiped 0:8ee38453bad9 122 static char tsc_cfg[2]={TSC_CFG,( TSC_AVG4
exopiped 0:8ee38453bad9 123 | TSC_DLY_1MS
exopiped 0:8ee38453bad9 124 | TSC_STL_1MS )};
exopiped 0:8ee38453bad9 125 static char tsc_enable[2]={TSC_CTRL,3};
exopiped 0:8ee38453bad9 126 static char tsc_ctrl=TSC_CTRL;
exopiped 0:8ee38453bad9 127 static char tsc_i_drive[2]={TSC_I_DRIVE,MAMP_50};
exopiped 0:8ee38453bad9 128 static char fifo_th[2]={FIFO_TH,1};
exopiped 0:8ee38453bad9 129 static char fifo_ctrl_sta=FIFO_STA;
exopiped 0:8ee38453bad9 130 static char tsc_data=TSC_DATA;
exopiped 0:8ee38453bad9 131
exopiped 0:8ee38453bad9 132 static int touch_audio_freq = 1000;
exopiped 0:8ee38453bad9 133 static int touch_audio_amplitude = 0;
exopiped 0:8ee38453bad9 134 static char touch_status=0; // result of reading TSC_CTR
exopiped 0:8ee38453bad9 135 static char fifo_status=0; // result of reading FIFO_CTRL_STA
exopiped 0:8ee38453bad9 136
exopiped 0:8ee38453bad9 137 short touch_x,touch_y;
exopiped 0:8ee38453bad9 138
exopiped 0:8ee38453bad9 139 // used by state machine that debounces touch detection
exopiped 0:8ee38453bad9 140 TOUCH_STATE touch_state=TOUCH_NOTOUCH;
exopiped 1:aa184d2eb2e3 141 #define GOOD_TOUCH_COUNT 4
exopiped 1:aa184d2eb2e3 142 #define NO_TOUCH_COUNT 4
exopiped 1:aa184d2eb2e3 143
exopiped 1:aa184d2eb2e3 144 bool touch_init(void)
exopiped 1:aa184d2eb2e3 145 {
exopiped 1:aa184d2eb2e3 146 touch_reset();
exopiped 1:aa184d2eb2e3 147 touch_state = TOUCH_NOTOUCH;
exopiped 1:aa184d2eb2e3 148 return true;
exopiped 1:aa184d2eb2e3 149 }
exopiped 0:8ee38453bad9 150
exopiped 1:aa184d2eb2e3 151 void touch_reset(void)
exopiped 0:8ee38453bad9 152 {
exopiped 0:8ee38453bad9 153 char chipid[2];
exopiped 0:8ee38453bad9 154
exopiped 0:8ee38453bad9 155 i2c_init(&touch_ctrl,p28,p27);
exopiped 0:8ee38453bad9 156 i2c_frequency(&touch_ctrl,100000);
exopiped 0:8ee38453bad9 157 wait_ms(1);
exopiped 0:8ee38453bad9 158
exopiped 0:8ee38453bad9 159 // read chip id
exopiped 0:8ee38453bad9 160 i2c_write(&touch_ctrl,TSC_ADDR,CHIP_ID,2,0);
exopiped 0:8ee38453bad9 161 i2c_read(&touch_ctrl,TSC_ADDR,chipid,2,1);
exopiped 0:8ee38453bad9 162 wait_ms(1);
exopiped 0:8ee38453bad9 163
exopiped 0:8ee38453bad9 164 // reset touch screen chip
exopiped 0:8ee38453bad9 165 i2c_write(&touch_ctrl,TSC_ADDR,tsc_reset,2,1);
exopiped 0:8ee38453bad9 166 wait_ms(5);
exopiped 0:8ee38453bad9 167
exopiped 0:8ee38453bad9 168 i2c_write(&touch_ctrl,TSC_ADDR,tsc_i_drive,2,1);
exopiped 0:8ee38453bad9 169 wait_ms(1);
exopiped 0:8ee38453bad9 170
exopiped 0:8ee38453bad9 171 // turn on ADC and TSC clocks
exopiped 0:8ee38453bad9 172 i2c_write(&touch_ctrl,TSC_ADDR,clox_on,2,1);
exopiped 0:8ee38453bad9 173 wait_ms(3);
exopiped 0:8ee38453bad9 174
exopiped 0:8ee38453bad9 175 // enable touch interrupt
exopiped 0:8ee38453bad9 176 i2c_write(&touch_ctrl,TSC_ADDR,touch_int_en,2,1);
exopiped 0:8ee38453bad9 177 wait_ms(1);
exopiped 0:8ee38453bad9 178
exopiped 0:8ee38453bad9 179 // 80 clock cycles for ADC conv, 12 bit ADC, internal ref
exopiped 0:8ee38453bad9 180 i2c_write(&touch_ctrl,TSC_ADDR,adc_ctrl1,2,1);
exopiped 0:8ee38453bad9 181 wait_ms(2);
exopiped 0:8ee38453bad9 182
exopiped 0:8ee38453bad9 183 // ADC clock = 3.25 MHz
exopiped 0:8ee38453bad9 184 i2c_write(&touch_ctrl,TSC_ADDR,adc_ctrl2,2,1);
exopiped 0:8ee38453bad9 185 wait_ms(1);
exopiped 0:8ee38453bad9 186
exopiped 0:8ee38453bad9 187 // 4 sample averaging and 1ms delays
exopiped 0:8ee38453bad9 188 i2c_write(&touch_ctrl,TSC_ADDR,tsc_cfg,2,1);
exopiped 0:8ee38453bad9 189 wait_ms(1);
exopiped 0:8ee38453bad9 190
exopiped 0:8ee38453bad9 191 // gpio alt function register to 0
exopiped 0:8ee38453bad9 192 i2c_write(&touch_ctrl,TSC_ADDR,gpio_af,2,1);
exopiped 0:8ee38453bad9 193 wait_ms(1);
exopiped 0:8ee38453bad9 194
exopiped 0:8ee38453bad9 195 // FIFO threshold not zero
exopiped 0:8ee38453bad9 196 i2c_write(&touch_ctrl,TSC_ADDR,fifo_th,2,1);
exopiped 0:8ee38453bad9 197 wait_ms(1);
exopiped 0:8ee38453bad9 198
exopiped 0:8ee38453bad9 199 // FIFO Reset
exopiped 0:8ee38453bad9 200 i2c_write(&touch_ctrl,TSC_ADDR,fifo_clear,2,1);
exopiped 0:8ee38453bad9 201 wait_ms(1);
exopiped 0:8ee38453bad9 202
exopiped 0:8ee38453bad9 203 // FIFO out of reset
exopiped 0:8ee38453bad9 204 i2c_write(&touch_ctrl,TSC_ADDR,fifo_operate,2,1);
exopiped 0:8ee38453bad9 205 wait_ms(1);
exopiped 0:8ee38453bad9 206
exopiped 0:8ee38453bad9 207 // enable touchscreen, no window tracking, x,y mode
exopiped 0:8ee38453bad9 208 i2c_write(&touch_ctrl,TSC_ADDR,tsc_enable,2,1);
exopiped 0:8ee38453bad9 209 wait_ms(1);
exopiped 0:8ee38453bad9 210
exopiped 0:8ee38453bad9 211 i2c_write(&touch_ctrl,TSC_ADDR,clr_intrupts,2,1);
exopiped 0:8ee38453bad9 212 wait_ms(1);
exopiped 0:8ee38453bad9 213
exopiped 0:8ee38453bad9 214 i2c_write(&touch_ctrl,TSC_ADDR,ena_intrupt,2,1);
exopiped 0:8ee38453bad9 215 wait_ms(1);
exopiped 0:8ee38453bad9 216 }
exopiped 0:8ee38453bad9 217
exopiped 0:8ee38453bad9 218 bool touch_debounce(void)
exopiped 0:8ee38453bad9 219 {
exopiped 0:8ee38453bad9 220 static int debounce_count=0;
exopiped 1:aa184d2eb2e3 221 int tret;
exopiped 0:8ee38453bad9 222
exopiped 1:aa184d2eb2e3 223 tret=touch_present();
exopiped 0:8ee38453bad9 224 switch (touch_state) {
exopiped 0:8ee38453bad9 225 case TOUCH_NOTOUCH:
exopiped 1:aa184d2eb2e3 226 if (tret>0) {
exopiped 0:8ee38453bad9 227 debounce_count=0;
exopiped 0:8ee38453bad9 228 touch_state = TOUCH_DEBOUNCE;
exopiped 0:8ee38453bad9 229 }
exopiped 0:8ee38453bad9 230 break;
exopiped 0:8ee38453bad9 231
exopiped 0:8ee38453bad9 232 case TOUCH_DEBOUNCE:
exopiped 1:aa184d2eb2e3 233 if (tret>0) {
exopiped 0:8ee38453bad9 234 if (++debounce_count > GOOD_TOUCH_COUNT) {
exopiped 0:8ee38453bad9 235 touch_state = TOUCH_PRESENT;
exopiped 1:aa184d2eb2e3 236 led1=1;
exopiped 0:8ee38453bad9 237 }
exopiped 1:aa184d2eb2e3 238 } else if(tret == 0) {
exopiped 0:8ee38453bad9 239 touch_state = TOUCH_NOTOUCH;
exopiped 0:8ee38453bad9 240 }
exopiped 0:8ee38453bad9 241 break;
exopiped 1:aa184d2eb2e3 242
exopiped 1:aa184d2eb2e3 243 case NOTOUCH_DEBOUNCE:
exopiped 1:aa184d2eb2e3 244 if(tret>0) {
exopiped 1:aa184d2eb2e3 245 touch_state = TOUCH_PRESENT;
exopiped 1:aa184d2eb2e3 246 } else if(tret== 0) {
exopiped 1:aa184d2eb2e3 247 if(++debounce_count > NO_TOUCH_COUNT) {
exopiped 1:aa184d2eb2e3 248 debounce_count=0;
exopiped 1:aa184d2eb2e3 249 touch_state=TOUCH_NOTOUCH;
exopiped 1:aa184d2eb2e3 250 }
exopiped 1:aa184d2eb2e3 251 }
exopiped 1:aa184d2eb2e3 252 break;
exopiped 0:8ee38453bad9 253
exopiped 0:8ee38453bad9 254 case TOUCH_PRESENT:
exopiped 1:aa184d2eb2e3 255 if (tret>0) {
exopiped 0:8ee38453bad9 256 touch_compute_params();
exopiped 1:aa184d2eb2e3 257 } else if(tret==0) {
exopiped 1:aa184d2eb2e3 258 debounce_count=0;
exopiped 1:aa184d2eb2e3 259 touch_state=NOTOUCH_DEBOUNCE;
exopiped 0:8ee38453bad9 260 }
exopiped 0:8ee38453bad9 261 break;
exopiped 0:8ee38453bad9 262
exopiped 0:8ee38453bad9 263 }
exopiped 1:aa184d2eb2e3 264 return ((touch_state==TOUCH_PRESENT) || (touch_state==NOTOUCH_DEBOUNCE));
exopiped 0:8ee38453bad9 265 }
exopiped 0:8ee38453bad9 266
exopiped 0:8ee38453bad9 267 int touch_frequency(void)
exopiped 0:8ee38453bad9 268 {
exopiped 0:8ee38453bad9 269 return touch_audio_freq;
exopiped 0:8ee38453bad9 270 }
exopiped 0:8ee38453bad9 271
exopiped 0:8ee38453bad9 272 int touch_amplitude(void)
exopiped 0:8ee38453bad9 273 {
exopiped 0:8ee38453bad9 274 return touch_audio_amplitude;
exopiped 0:8ee38453bad9 275 }
exopiped 0:8ee38453bad9 276 void touch_compute_params(void)
exopiped 0:8ee38453bad9 277 {
exopiped 0:8ee38453bad9 278 if(0>touch_get_xy(&touch_x,&touch_y)) return;
exopiped 1:aa184d2eb2e3 279 if((touch_y>0x3F) && (touch_y!=0xFFF)) {
exopiped 1:aa184d2eb2e3 280 touch_audio_freq = (TOUCH_MIN_FREQUENCY
exopiped 0:8ee38453bad9 281 + (TOUCH_MAX_FREQUENCY - TOUCH_MIN_FREQUENCY)
exopiped 1:aa184d2eb2e3 282 * touch_y)>>12;
exopiped 1:aa184d2eb2e3 283 }
exopiped 1:aa184d2eb2e3 284 if((touch_x > 0x3F) && (touch_x != 0xFFF)) {
exopiped 1:aa184d2eb2e3 285 touch_audio_amplitude = (TOUCH_MAX_AMPLITUDE*touch_x)>>12;
exopiped 1:aa184d2eb2e3 286 }
exopiped 0:8ee38453bad9 287 }
exopiped 1:aa184d2eb2e3 288 /*
exopiped 1:aa184d2eb2e3 289 * return 1 if touch present, 0 if touch absent, and -1
exopiped 1:aa184d2eb2e3 290 * if there was an i2c error
exopiped 1:aa184d2eb2e3 291 */
exopiped 1:aa184d2eb2e3 292 int touch_present(void)
exopiped 0:8ee38453bad9 293 {
exopiped 1:aa184d2eb2e3 294 // <<<<< i2c_init(&touch_ctrl,p28,p27); // sync i2c routines
exopiped 1:aa184d2eb2e3 295 // <<<<< i2c_frequency(&touch_ctrl,100000); // 100kHz clock
exopiped 0:8ee38453bad9 296
exopiped 0:8ee38453bad9 297 i2c_write(&touch_ctrl,TSC_ADDR,&tsc_ctrl,1,0);
exopiped 0:8ee38453bad9 298 i2c_read(&touch_ctrl,TSC_ADDR,&touch_status,1,1);
exopiped 0:8ee38453bad9 299
exopiped 1:aa184d2eb2e3 300 if ((touch_status & TSC_EN_MASK)==0) { // i2c error
exopiped 0:8ee38453bad9 301 led4=1; // disables screen
exopiped 1:aa184d2eb2e3 302 touch_reset(); // re-init fixes
exopiped 1:aa184d2eb2e3 303 return -1;
exopiped 0:8ee38453bad9 304 } else if ((touch_status & TSC_TOUCH_DET)>0) {
exopiped 1:aa184d2eb2e3 305 return 1;
exopiped 0:8ee38453bad9 306 }
exopiped 1:aa184d2eb2e3 307 return 0;
exopiped 0:8ee38453bad9 308 }
exopiped 0:8ee38453bad9 309
exopiped 0:8ee38453bad9 310 int touch_get_xy(short *x,short *y)
exopiped 0:8ee38453bad9 311 {
exopiped 0:8ee38453bad9 312 unsigned char packed_xy[3];
exopiped 0:8ee38453bad9 313
exopiped 0:8ee38453bad9 314 if(i2c_write(&touch_ctrl,TSC_ADDR,&fifo_ctrl_sta,1,0)<0)
exopiped 0:8ee38453bad9 315 return NIL;
exopiped 0:8ee38453bad9 316 if(i2c_read(&touch_ctrl,TSC_ADDR,&fifo_status,1,1)<0)
exopiped 0:8ee38453bad9 317 return NIL;
exopiped 0:8ee38453bad9 318 while ((fifo_status & FIFO_EMPTY)!=FIFO_EMPTY ) {
exopiped 0:8ee38453bad9 319 if (i2c_write(&touch_ctrl,TSC_ADDR,&tsc_data,1,0) < 0)
exopiped 0:8ee38453bad9 320 return NIL;
exopiped 0:8ee38453bad9 321 if(i2c_read(&touch_ctrl,TSC_ADDR,(char *)packed_xy,3,1)<0)
exopiped 0:8ee38453bad9 322 return NIL;
exopiped 0:8ee38453bad9 323 if(i2c_write(&touch_ctrl,TSC_ADDR,&fifo_ctrl_sta,1,0)<0)
exopiped 0:8ee38453bad9 324 return NIL;
exopiped 0:8ee38453bad9 325 if(i2c_read(&touch_ctrl,TSC_ADDR,&fifo_status,1,1)<0)
exopiped 0:8ee38453bad9 326 return NIL;
exopiped 0:8ee38453bad9 327 }
exopiped 0:8ee38453bad9 328 if(i2c_write(&touch_ctrl,TSC_ADDR,clr_intrupts,2,1)<0)
exopiped 0:8ee38453bad9 329 return NIL;
exopiped 0:8ee38453bad9 330 *x = (short)(packed_xy[0]<<4)
exopiped 0:8ee38453bad9 331 | (short)((packed_xy[1] & 0xF0)>>4);
exopiped 0:8ee38453bad9 332 *y = 0xFFF -((short)((packed_xy[1] & 0x0F)<<8)
exopiped 0:8ee38453bad9 333 | (short)(packed_xy[2]));
exopiped 0:8ee38453bad9 334 // 0xFFF or 0x000 is some kind of glitch
exopiped 0:8ee38453bad9 335 if (*x==0xFFF && *y==0xFFF) return NIL;
exopiped 0:8ee38453bad9 336 if (*x==0 && *y==0) return NIL;
exopiped 2:c5eeaf1c8e69 337 // debug_hexshort(*x);
exopiped 2:c5eeaf1c8e69 338 // debug_putch(',');
exopiped 2:c5eeaf1c8e69 339 // debug_hexshort(*y);
exopiped 2:c5eeaf1c8e69 340 // debug_crlf();
exopiped 0:8ee38453bad9 341 return 0;
exopiped 0:8ee38453bad9 342 }
exopiped 0:8ee38453bad9 343