summaryrefslogtreecommitdiff
path: root/drivers/tsd/tsd_ads7843.c
blob: 3800a5f9413ce1148b4ff388450cca8c643fe125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include <board.h>

#ifdef BOARD_TSC_ADS7843

#include "tsd.h"
#include "tsd_com.h"
#include <irq/irq.h>
#include <pio/pio.h>
#include <pio/pio_it.h>
#if defined(cortexm3)
#include <systick/systick.h>
#else
#include <pit/pit.h>
#endif
#include <ads7843/ads7843.h>
#include <drivers/lcd/lcdd.h>
#include <drivers/lcd/draw.h>
#include <drivers/lcd/font.h>

#include <string.h>

//------------------------------------------------------------------------------
//         Local definitions
//------------------------------------------------------------------------------

/// PIT period value in µseconds.
#define PIT_PERIOD          10000 // 10 ms

/// Delay for pushbutton debouncing (in PIT_PERIOD time-base).
#define DEBOUNCE_TIME       6 // PIT_PERIOD * 6 = 60 ms

/// Color of calibration points.
#define POINTS_COLOR        0x0000FF

/// Size in pixels of calibration points.
#define POINTS_SIZE         4

/// Maximum difference in pixels between the test point and the measured point.
#define POINTS_MAX_ERROR    5

//------------------------------------------------------------------------------
//         Local types
//------------------------------------------------------------------------------

/// pen state
typedef enum {
    STATE_PEN_RELEASED  = 0,
    STATE_PEN_PRESSED   = 1,
    STATE_PEN_DEBOUNCE  = 2
} e_pen_state;


//------------------------------------------------------------------------------
//         Local variables
//------------------------------------------------------------------------------

/// Pins used by Interrupt Signal for Touch Screen Controller
static Pin pinPenIRQ  = PIN_TCS_IRQ;

/// Global timestamp in milliseconds since start of application.
static volatile unsigned int timestamp = 0;

/// last time when the pen is pressed on the touchscreen
static volatile unsigned int timePress = 0;

/// last time when the pen is released
static volatile unsigned int timeRelease = 0;

/// pen state
static volatile e_pen_state penState = STATE_PEN_RELEASED;

/// Touch screen initiallized flag 
static unsigned int tsInitFlag = 0;

//------------------------------------------------------------------------------
//         External functions
//------------------------------------------------------------------------------

extern void TSD_PenPressed(unsigned int x, unsigned int y);
extern void TSD_PenMoved(unsigned int x, unsigned int y);
extern void TSD_PenReleased(unsigned int x, unsigned int y);

//------------------------------------------------------------------------------
//         Local functions
//------------------------------------------------------------------------------
extern void TSD_GetRawMeasurement(unsigned int *pData);

#if defined(cortexm3)
//------------------------------------------------------------------------------
/// Timer handler for touch screen. Increments the timestamp counter.
/// Determine the state "Pen Pressed" or "Pen Released". To change state,
/// the penIRQ has to keep the same value during DEBOUNCE_TIME.
/// For AT91SAM3, SysTick interrupt should call it per 10ms.
//------------------------------------------------------------------------------
void TSD_TimerHandler(void)
{
    unsigned int data[2];
    static unsigned int point[2];

    if (!tsInitFlag) return;

    timestamp++;
    // Get the current position of the pen if penIRQ has low value (pen pressed)
    if (PIO_Get(&pinPenIRQ) == 0) {

        // Get the current position of the pressed pen
        if(TSDCom_IsCalibrationOk()) {
            TSD_GetRawMeasurement(data);
            TSDCom_InterpolateMeasurement(data, point);
        }

        // call the callback function
        if(penState == STATE_PEN_PRESSED) {
            if(TSDCom_IsCalibrationOk()) {
                TSD_PenMoved(point[0], point[1]);
            }
        }
    }

    // Determine the pen state
    if (PIO_Get(&pinPenIRQ) == 0) {

        // reinit the last time when release
        timeRelease = timestamp;

        if(penState == STATE_PEN_DEBOUNCE) {

            if( (timestamp - timePress) > DEBOUNCE_TIME) {

                // pen is pressed during an enough time : the state change
                penState = STATE_PEN_PRESSED;
                // call the callback function
                if(TSDCom_IsCalibrationOk()) {
                    TSD_PenPressed(point[0], point[1]);
                }
            }
        }
    }
    else {
        // reinit the last time when release
        timePress = timestamp;

        if(penState == STATE_PEN_DEBOUNCE) {

            if( (timestamp - timeRelease) > DEBOUNCE_TIME) {

                // pen is released during an enough time : the state change
                penState = STATE_PEN_RELEASED;
                // call the callback function
                if(TSDCom_IsCalibrationOk()) {
                    TSD_PenReleased(point[0], point[1]);
                }
            }
        }
    }
}

#else // For SAM7/SAM9

//------------------------------------------------------------------------------
/// Handler for PIT interrupt. Increments the timestamp counter.
/// Determine the state "Pen Pressed" or "Pen Released". To change state,
/// the penIRQ has to keep the same value during DEBOUNCE_TIME.
//------------------------------------------------------------------------------
static void ISR_Pit(void)
{
    unsigned int status;
    unsigned int data[2];
    static unsigned int point[2];

    // Read the PIT status register
    status = PIT_GetStatus() & AT91C_PITC_PITS;
    if (status != 0) {

        // Read the PIVR to acknowledge interrupt and get number of ticks
        timestamp += (PIT_GetPIVR() >> 20);
    }

    // Get the current position of the pen if penIRQ has low value (pen pressed)
    if (PIO_Get(&pinPenIRQ) == 0) {

        // Get the current position of the pressed pen
        PIO_DisableIt(&pinPenIRQ);
        ADS7843_GetPosition(&data[0], &data[1]);
        PIO_EnableIt(&pinPenIRQ);

        TSD_GetRawMeasurement(data);
        TSDCom_InterpolateMeasurement(data, point);

        // call the callback function
        if(penState == STATE_PEN_PRESSED) {
            if(TSDCom_IsCalibrationOk()) {
                TSD_PenMoved(point[0], point[1]);
            }
        }
    }

    // Determine the pen state
    if (PIO_Get(&pinPenIRQ) == 0) {

        // reinit the last time when release
        timeRelease = timestamp;

        if(penState == STATE_PEN_DEBOUNCE) {

            if( (timestamp - timePress) > DEBOUNCE_TIME) {

                // pen is pressed during an enough time : the state change
                penState = STATE_PEN_PRESSED;
                // call the callback function
                if(TSDCom_IsCalibrationOk()) {
                    TSD_PenPressed(point[0], point[1]);
                }
            }
        }
    }
    else {
        // reinit the last time when release
        timePress = timestamp;

        if(penState == STATE_PEN_DEBOUNCE) {

            if( (timestamp - timeRelease) > DEBOUNCE_TIME) {

                // pen is released during an enough time : the state change
                penState = STATE_PEN_RELEASED;
                // call the callback function
                if(TSDCom_IsCalibrationOk()) {
                    TSD_PenReleased(point[0], point[1]);
                }
            }
        }
    }
}
#endif

//------------------------------------------------------------------------------
/// Interrupt handler for Touchscreen.
//------------------------------------------------------------------------------
static void ISR_PenIRQ(void)
{
    // Check if the pen has been pressed
    if (!PIO_Get(&pinPenIRQ)) {

        if(penState == STATE_PEN_RELEASED) {

            timePress = timestamp;
            penState = STATE_PEN_DEBOUNCE;
        }
    }
    else {

        if(penState == STATE_PEN_PRESSED) {

            timeRelease = timestamp;
            penState = STATE_PEN_DEBOUNCE;
        }
    }
}

#if !defined(cortexm3)
//------------------------------------------------------------------------------
/// Configure the periodic interval timer to generate an interrupt every 10 ms
//------------------------------------------------------------------------------
static void ConfigurePit(void)
{
    // Initialize the PIT to the desired frequency
    PIT_Init(PIT_PERIOD, BOARD_MCK / 1000000);

    // Configure interrupt on PIT
    IRQ_DisableIT(AT91C_ID_SYS);
    IRQ_ConfigureIT(AT91C_ID_SYS, AT91C_AIC_PRIOR_LOWEST, ISR_Pit);
    IRQ_EnableIT(AT91C_ID_SYS);
    PIT_EnableIT();

    // Enable the pit
    PIT_Enable();
}
#endif

//-----------------------------------------------------------------------------
/// Configure PENIRQ for interrupt
//-----------------------------------------------------------------------------
void ConfigurePenIRQ(void)
{
    // Configure pios
    PIO_Configure(&pinPenIRQ, PIO_LISTSIZE(pinPenIRQ));

    // Initialize interrupts
#if defined(cortexm3)
    PIO_InitializeInterrupts(0);
#else    // For SAM7/SAM9
    PIO_InitializeInterrupts(AT91C_AIC_PRIOR_HIGHEST);
#endif
    PIO_ConfigureIt(&pinPenIRQ, (void (*)(const Pin *)) ISR_PenIRQ);

    // Enable the interrupt
    PIO_EnableIt(&pinPenIRQ);
}

//------------------------------------------------------------------------------
//         Global functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Reads and store a touchscreen measurement in the provided array.
/// \param pData  Array where the measurements will be stored
//------------------------------------------------------------------------------
void TSD_GetRawMeasurement(unsigned int *pData)
{
#if defined(cortexm3)
    // Get the current position of the pressed pen
    PIO_DisableIt(&pinPenIRQ);
    ADS7843_GetPosition(&pData[0], &pData[1]);
    PIO_EnableIt(&pinPenIRQ);
#else
    // Get the current position of the pressed pen
    PIO_DisableIt(&pinPenIRQ);
    ADS7843_GetPosition(&pData[0], &pData[1]);
    PIO_EnableIt(&pinPenIRQ);
#endif
}

//------------------------------------------------------------------------------
/// Wait pen pressed
//------------------------------------------------------------------------------
void TSD_WaitPenPressed(void)
{
    // Wait for touch & end of conversion
    while (penState != STATE_PEN_RELEASED);
    while (penState != STATE_PEN_PRESSED);
}

//------------------------------------------------------------------------------
/// Wait pen released
//------------------------------------------------------------------------------
void TSD_WaitPenReleased(void)
{
    // Wait for contact loss
    while (penState != STATE_PEN_PRESSED);
    while (penState != STATE_PEN_RELEASED);
}

//------------------------------------------------------------------------------
/// Do calibration
/// \param pLcdBuffer  LCD buffer to use for displaying the calibration info.
/// \return 1 if calibration is Ok, 0 else
//------------------------------------------------------------------------------
unsigned char TSD_Calibrate(void *pLcdBuffer)
{
    unsigned char ret = 0;

    // Calibration is done only once
    if(TSDCom_IsCalibrationOk()) {
        return 1;
    }

    // Do calibration
    ret = TSDCom_Calibrate(pLcdBuffer);

    return ret;
}

//------------------------------------------------------------------------------
/// Initializes the touchscreen driver and starts the calibration process. When
/// finished, the touchscreen is operational.
///
/// Important: the LCD driver must have been initialized prior to calling this
/// function.
/// \param pBuffer  LCD buffer to use for displaying the calibration info.
//------------------------------------------------------------------------------
void TSD_Initialize(void *pLcdBuffer)
{
    ADS7843_Initialize();
    ConfigurePenIRQ();
#if !defined(cortexm3)
    ConfigurePit();
#endif
    tsInitFlag = 1;

    // Calibration
    if(pLcdBuffer) {
       while (!TSD_Calibrate(pLcdBuffer));
    }
}

//------------------------------------------------------------------------------
/// Stop the Touchscreen, disable interrupt and stop Pit
//------------------------------------------------------------------------------
void TSD_Reset(void)
{
    // Disable SPI 0
    ADS7843_Reset();

    // Disable the interrupt
    PIO_DisableIt(&pinPenIRQ);

#if !defined(cortexm3)
    // Stop Pit interrupt
    PIT_DisableIT();
    IRQ_DisableIT(AT91C_ID_SYS);
#endif
}

#endif //#ifdef BOARD_TSC_ADS7843
personal git repositories of Harald Welte. Your mileage may vary