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
|
/* ----------------------------------------------------------------------------
* 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 AT91C_BASE_TSADC
#include "tsd.h"
#include "tsd_com.h"
#include <irq/irq.h>
#include <pio/pio.h>
#include <tsadcc/tsadcc.h>
#include <drivers/lcd/lcdd.h>
#include <drivers/lcd/draw.h>
#include <drivers/lcd/font.h>
#include <string.h>
//------------------------------------------------------------------------------
// Local definitions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Local types
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Local variables
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// 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);
//------------------------------------------------------------------------------
/// Interrupt handler for the TSADC. Handles pen press, pen move and pen release
/// events by invoking three callback functions.
//------------------------------------------------------------------------------
static void InterruptHandler(void)
{
unsigned int status;
unsigned int data[2];
static unsigned int newPoint[2];
static unsigned int point[2];
static unsigned char report = 0;
// Retrieve TADC status
// Bug fix: two step operation so IAR doesn't complain that the order of
// volatile access is unspecified.
status = AT91C_BASE_TSADC->TSADC_SR;
status &= AT91C_BASE_TSADC->TSADC_IMR;
// Pen release
if ((status & AT91C_TSADC_NOCNT) == AT91C_TSADC_NOCNT) {
// Invoke PenReleased callback
if(TSDCom_IsCalibrationOk()) {
TSD_PenReleased(point[0], point[1]);
}
// Stop periodic trigger mode
TSADCC_SetDebounceTime(BOARD_TOUCHSCREEN_DEBOUNCE);
AT91C_BASE_TSADC->TSADC_IDR = AT91C_TSADC_EOC3 | AT91C_TSADC_NOCNT;
TSADCC_SetTriggerMode(AT91C_TSADC_TRGMOD_PENDET_TRIGGER);
TSD_GetRawMeasurement(data); // Clear data registers
AT91C_BASE_TSADC->TSADC_SR;
AT91C_BASE_TSADC->TSADC_IER = AT91C_TSADC_PENCNT;
}
// Pen press
else if ((status & AT91C_TSADC_PENCNT) == AT91C_TSADC_PENCNT) {
// Invoke PenPressed callback with (x,y) coordinates
while ((AT91C_BASE_TSADC->TSADC_SR & AT91C_TSADC_EOC3) != AT91C_TSADC_EOC3);
TSD_GetRawMeasurement(data);
TSDCom_InterpolateMeasurement(data, point);
// Invoke PenPressed callback
if(TSDCom_IsCalibrationOk()) {
TSD_PenPressed(point[0], point[1]);
}
// Configure TSADC for periodic trigger
TSADCC_SetDebounceTime(10); // 1ns
AT91C_BASE_TSADC->TSADC_IER = AT91C_TSADC_EOC3 | AT91C_TSADC_NOCNT;
AT91C_BASE_TSADC->TSADC_IDR = AT91C_TSADC_PENCNT;
TSADCC_SetTriggerPeriod(10000000); // 10ms
TSADCC_SetTriggerMode(AT91C_TSADC_TRGMOD_PERIODIC_TRIGGER);
report = 0;
}
// Pen move
else if ((status & AT91C_TSADC_EOC3) == AT91C_TSADC_EOC3) {
// Invoke callback with LAST value measured
// Explanation: the very last value that will be measured (just as the
// pen is released) might be corrupted and there is no way to know. So
// we just discard it.
if (report) {
memcpy(point, newPoint, sizeof(newPoint));
// Invoke PenMoved callback
if(TSDCom_IsCalibrationOk()) {
TSD_PenMoved(point[0], point[1]);
}
report = 0;
}
TSD_GetRawMeasurement(data);
TSDCom_InterpolateMeasurement(data, newPoint);
if ((newPoint[0] != point[0]) || (newPoint[1] != point[1])) {
report = 1;
}
}
}
//------------------------------------------------------------------------------
// Global functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Reads and store a touchscreen measurement in the provided array.
/// The value stored are:
/// - data[0] = CDR3 * 1024 / CDR2
/// - data[1] = CDR1 * 1024 / CDR0
/// \param pData Array where the measurements will be stored
//------------------------------------------------------------------------------
void TSD_GetRawMeasurement(unsigned int *pData)
{
pData[0] = (AT91C_BASE_TSADC->TSADC_CDR3 * 1024);
pData[0] /= AT91C_BASE_TSADC->TSADC_CDR2;
pData[1] = (AT91C_BASE_TSADC->TSADC_CDR1 * 1024);
pData[1] /= AT91C_BASE_TSADC->TSADC_CDR0;
}
//------------------------------------------------------------------------------
/// Wait pen pressed
//------------------------------------------------------------------------------
void TSD_WaitPenPressed(void)
{
// Wait for touch & end of conversion
while ((AT91C_BASE_TSADC->TSADC_SR & AT91C_TSADC_PENCNT) != AT91C_TSADC_PENCNT);
while ((AT91C_BASE_TSADC->TSADC_SR & AT91C_TSADC_EOC3) != AT91C_TSADC_EOC3);
}
//------------------------------------------------------------------------------
/// Wait pen released
//------------------------------------------------------------------------------
void TSD_WaitPenReleased(void)
{
// Wait for contact loss
while ((AT91C_BASE_TSADC->TSADC_SR & AT91C_TSADC_NOCNT) != AT91C_TSADC_NOCNT);
}
//------------------------------------------------------------------------------
/// 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;
}
// Enable trigger
TSADCC_SetTriggerMode(AT91C_TSADC_TRGMOD_PENDET_TRIGGER);
// Do calibration
ret = TSDCom_Calibrate(pLcdBuffer);
// Disable trigger
TSADCC_SetTriggerMode(AT91C_TSADC_TRGMOD_NO_TRIGGER);
// Configure interrupt generation
// Do it only if the calibration is Ok.
if(ret) {
TSADCC_SetTriggerMode(AT91C_TSADC_TRGMOD_PENDET_TRIGGER);
IRQ_ConfigureIT(AT91C_ID_TSADC, 0, InterruptHandler);
IRQ_EnableIT(AT91C_ID_TSADC);
AT91C_BASE_TSADC->TSADC_IER = AT91C_TSADC_PENCNT;
}
return ret;
}
//------------------------------------------------------------------------------
/// Initializes the touchscreen driver and starts the calibration process. When
/// finished, the touchscreen is operational.
/// The configuration is taken from the board.h of the device being compiled.
///
/// Important: the LCD driver must have been initialized prior to calling this
/// function.
/// \param pLcdBuffer LCD buffer to use for displaying the calibration info.
//------------------------------------------------------------------------------
void TSD_Initialize(void *pLcdBuffer)
{
const Pin pins[] = {PINS_TSADCC};
// Configuration
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_TSADC;
PIO_Configure(pins, PIO_LISTSIZE(pins));
TSADCC_SetOperatingMode(AT91C_TSADC_TSAMOD_TS_ONLY_MODE);
TSADCC_SetPenDetect(1);
TSADCC_SetAdcFrequency(BOARD_TOUCHSCREEN_ADCCLK, BOARD_MCK);
TSADCC_SetStartupTime(BOARD_TOUCHSCREEN_STARTUP);
TSADCC_SetTrackAndHoldTime(BOARD_TOUCHSCREEN_SHTIM);
TSADCC_SetDebounceTime(BOARD_TOUCHSCREEN_DEBOUNCE);
// Do calibration if the LCd buffer is not a null pointer
if(pLcdBuffer) {
// Calibration
while (!TSD_Calibrate(pLcdBuffer));
}
}
//------------------------------------------------------------------------------
/// Reset/stop the touchscreen
//------------------------------------------------------------------------------
void TSD_Reset(void)
{
IRQ_DisableIT(AT91C_ID_TSADC);
AT91C_BASE_PMC->PMC_PCDR = 1 << AT91C_ID_TSADC;
}
#endif //#ifdef AT91C_BASE_TSADC
|