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
|
/* ----------------------------------------------------------------------------
* 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.
* ----------------------------------------------------------------------------
*/
//------------------------------------------------------------------------------
/// \unit
///
/// !Purpose
///
/// ADS7843 driver
///
/// !Usage
///
/// Explanation on the usage of the code made available through the header file.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Headers
//------------------------------------------------------------------------------
#include <board.h>
#ifdef BOARD_TSC_ADS7843
#include <pio/pio.h>
#include <spi/spi.h>
#include "ads7843.h"
//------------------------------------------------------------------------------
// Defines
//------------------------------------------------------------------------------
#define ADS_CTRL_PD0 (1 << 0) // PD0
#define ADS_CTRL_PD1 (1 << 1) // PD1
#define ADS_CTRL_DFR (1 << 2) // SER/DFR
#define ADS_CTRL_EIGHT_BITS_MOD (1 << 3) // Mode
#define ADS_CTRL_START (1 << 7) // Start Bit
#define ADS_CTRL_SWITCH_SHIFT 4 // Address setting
#if defined(cortexm3)
// Get X position command
#define CMD_Y_POSITION ((1 << ADS_CTRL_SWITCH_SHIFT) | ADS_CTRL_START | ADS_CTRL_PD0 | ADS_CTRL_PD1)
// Get Y position command
#define CMD_X_POSITION ((5 << ADS_CTRL_SWITCH_SHIFT) | ADS_CTRL_START | ADS_CTRL_PD0 | ADS_CTRL_PD1)
#else
// Get X position command
#define CMD_X_POSITION ((1 << ADS_CTRL_SWITCH_SHIFT) | ADS_CTRL_START | ADS_CTRL_PD0 | ADS_CTRL_PD1)
// Get Y position command
#define CMD_Y_POSITION ((5 << ADS_CTRL_SWITCH_SHIFT) | ADS_CTRL_START | ADS_CTRL_PD0 | ADS_CTRL_PD1)
#endif
// Enable penIRQ
#define CMD_ENABLE_PENIRQ ((1 << ADS_CTRL_SWITCH_SHIFT) | ADS_CTRL_START)
#define AT91C_TOUCHSCREEN_TIMEOUT 5000000
#define DELAY_BEFORE_SPCK (200 << 16) // 2us min (tCSS) <=> 200/100 000 000 = 2us
#define DELAY_BETWEEN_CONS_COM (0xf << 24) // 5us min (tCSH) <=> (32 * 15) / (100 000 000) = 5us
//------------------------------------------------------------------------------
// Local variables
//------------------------------------------------------------------------------
/// Pins used by SPI
static const Pin pinsSPI[] = {BOARD_TSC_SPI_PINS, BOARD_TSC_NPCS_PIN};
/// Touch screen BUSY pin
static const Pin pinBusy[] = {PIN_TCS_BUSY};
/// Touch screen CS pin
static const Pin pinNss[] = {BOARD_TSC_NPCS_PIN};
//------------------------------------------------------------------------------
// Local functions
//------------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/// Generic function to send a command to the touchscreen controller
/// \param bCmd command to send
/// \return command result
//-----------------------------------------------------------------------------
static unsigned int SendCommand(unsigned char bCmd)
{
#if defined(cortexm3)
int i;
AT91S_SPI *spi = BOARD_TSC_SPI_BASE;
unsigned int uResult = 0;
// (volatile declaration needed for code optimisation by compiler)
volatile unsigned char bufferRX[3];
volatile unsigned char bufferTX[3];
bufferRX[0] = 0;
bufferRX[1] = 0;
bufferRX[2] = 0;
bufferTX[0] = bCmd;
bufferTX[1] = 0;
bufferTX[2] = 0;
// Send command
i = 0;
while ((spi->SPI_SR & AT91C_SPI_TXEMPTY) == 0);
spi->SPI_TDR = bufferTX[i] | SPI_PCS(BOARD_TSC_NPCS);
while ((spi->SPI_SR & AT91C_SPI_TDRE) == 0);
while (PIO_Get(pinBusy) == 1);
while ((spi->SPI_SR & AT91C_SPI_RDRF) == 0);
bufferRX[i] = spi->SPI_RDR & 0xFFFF;
// Read data
for (i = 1; i < 3; i++) {
while ((spi->SPI_SR & AT91C_SPI_TXEMPTY) == 0);
spi->SPI_TDR = bufferTX[i] | SPI_PCS(BOARD_TSC_NPCS);
while ((spi->SPI_SR & AT91C_SPI_TDRE) == 0);
while ((spi->SPI_SR & AT91C_SPI_RDRF) == 0);
bufferRX[i] = spi->SPI_RDR & 0xFFFF;
}
uResult = ((unsigned int)bufferRX[1] << 8) | (unsigned int)bufferRX[2];
uResult = uResult >> 4;
return uResult;
#else // AT91SAM7, SAM9
unsigned int uResult = 0;
unsigned int uTimeout = 0;
// (volatile declaration needed for code optimisation by compiler)
volatile unsigned char bufferRX[3];
volatile unsigned char bufferTX[3];
AT91PS_PDC pPdc = (AT91PS_PDC) &(AT91C_BASE_SPI0->SPI_RPR);
unsigned int dStatus;
bufferRX[0] = 0;
bufferRX[1] = 0;
bufferRX[2] = 0;
bufferTX[0] = bCmd;
bufferTX[1] = 0;
bufferTX[2] = 0;//bCmd;
// Send Command and data through the SPI
pPdc->PDC_PTCR = AT91C_PDC_RXTDIS;
pPdc->PDC_RPR = (unsigned int) bufferRX;
pPdc->PDC_RCR = 3;
pPdc->PDC_PTCR = AT91C_PDC_TXTDIS;
pPdc->PDC_TPR = (unsigned int) bufferTX;
pPdc->PDC_TCR = 3;
pPdc->PDC_PTCR = AT91C_PDC_RXTEN;
pPdc->PDC_PTCR = AT91C_PDC_TXTEN;
do {
dStatus = AT91C_BASE_SPI0->SPI_SR;
uTimeout++;
}
while ((( dStatus & AT91C_SPI_RXBUFF) != AT91C_SPI_RXBUFF) && (uTimeout < AT91C_TOUCHSCREEN_TIMEOUT));
pPdc->PDC_PTCR = AT91C_PDC_RXTDIS;
pPdc->PDC_PTCR = AT91C_PDC_TXTDIS;
uResult = ((unsigned int)bufferRX[1] << 8) | (unsigned int)bufferRX[2];
uResult = uResult >> 4;
return uResult;
#endif
}
//------------------------------------------------------------------------------
// Global functions
//------------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/// Get position of the pen by ask the ADS controller (SPI)
/// \param px_pos pointer to the horizontal position
/// \param py_pos pointer to the vertical position
//-----------------------------------------------------------------------------
void ADS7843_GetPosition(unsigned int* px_pos, unsigned int* py_pos)
{
// Get X position
*px_pos = SendCommand(CMD_X_POSITION);
// Get Y position
*py_pos = SendCommand(CMD_Y_POSITION);
// Switch to full power mode
SendCommand(CMD_ENABLE_PENIRQ);
}
//-----------------------------------------------------------------------------
/// Initialization of the SPI for communication with ADS7843 component
//-----------------------------------------------------------------------------
void ADS7843_Initialize(void)
{
volatile unsigned int uDummy;
// Configure pins
PIO_Configure(pinsSPI, PIO_LISTSIZE(pinsSPI));
PIO_Configure(pinBusy, PIO_LISTSIZE(pinBusy));
SPI_Configure(AT91C_BASE_SPI0,
AT91C_ID_SPI0,
AT91C_SPI_MSTR | AT91C_SPI_MODFDIS | SPI_PCS(BOARD_TSC_NPCS) // Value of the SPI configuration register.
);
#if defined(cortexm3)
SPI_ConfigureNPCS(AT91C_BASE_SPI0, BOARD_TSC_NPCS,
AT91C_SPI_NCPHA | (AT91C_SPI_DLYBS & DELAY_BEFORE_SPCK) | AT91C_SPI_CSAAT |
(AT91C_SPI_DLYBCT & DELAY_BETWEEN_CONS_COM) | (0xC8 << 8) );
#else // AT91SAM7, SAM9
SPI_ConfigureNPCS(AT91C_BASE_SPI0, BOARD_TSC_NPCS,
AT91C_SPI_NCPHA | (AT91C_SPI_DLYBS & DELAY_BEFORE_SPCK) |
(AT91C_SPI_DLYBCT & DELAY_BETWEEN_CONS_COM) | (0xC8 << 8) );
#endif
SPI_Enable(AT91C_BASE_SPI0);
for (uDummy=0; uDummy<100000; uDummy++);
uDummy = AT91C_BASE_SPI0->SPI_SR;
uDummy = AT91C_BASE_SPI0->SPI_RDR;
SendCommand(CMD_ENABLE_PENIRQ);
}
//-----------------------------------------------------------------------------
/// Reset the ADS7843
//-----------------------------------------------------------------------------
void ADS7843_Reset(void)
{
// Disable SPI 0
SPI_Disable(AT91C_BASE_SPI0);
}
#endif //#ifdef BOARD_TSC_ADS7843
|