eGUI alias D4D  Release 3.0
Reference Manual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
d4dtch_TSC2046.c
Go to the documentation of this file.
1 /**************************************************************************
2 *
3 * Copyright 2014 by Petr Gargulak. eGUI Community.
4 * Copyright 2009-2013 by Petr Gargulak. Freescale Semiconductor, Inc.
5 *
6 ***************************************************************************
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License Version 3
9 * or later (the "LGPL").
10 *
11 * As a special exception, the copyright holders of the eGUI project give you
12 * permission to link the eGUI sources with independent modules to produce an
13 * executable, regardless of the license terms of these independent modules,
14 * and to copy and distribute the resulting executable under terms of your
15 * choice, provided that you also meet, for each linked independent module,
16 * the terms and conditions of the license of that module.
17 * An independent module is a module which is not derived from or based
18 * on this library.
19 * If you modify the eGUI sources, you may extend this exception
20 * to your version of the eGUI sources, but you are not obligated
21 * to do so. If you do not wish to do so, delete this
22 * exception statement from your version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 *
28 * You should have received a copy of the GNU General Public License
29 * and the GNU Lesser General Public License along with this program.
30 * If not, see <http://www.gnu.org/licenses/>.
31 *
32 ***************************************************************************/
46 #include "d4d.h" // include of all public items (types, function etc) of D4D driver
47 #include "common_files/d4d_lldapi.h" // include non public low level driver interface header file (types, function prototypes, enums etc. )
48 #include "common_files/d4d_private.h" // include the private header file that contains perprocessor macros as D4D_MK_STR
49 
50 
51 // identification string of driver - must be same as name D4DTCH_FUNCTIONS structure + "_ID"
52 // it is used for enable the code for compilation
53 #define d4dtch_tsc2046_ID 1
54 
55 
56 // copilation enable preprocessor condition
57 // the string d4dtch_tsc2046_ID must be replaced by define created one line up
58 #if (D4D_MK_STR(D4D_LLD_TCH) == d4dtch_tsc2046_ID)
59 
60  // include of low level driver heaser file
61  // it will be included into wole project only in case that this driver is selected in main D4D configuration file
63  /******************************************************************************
64  * Macros
65  ******************************************************************************/
66 
67  /******************************************************************************
68  * Internal function prototypes
69  ******************************************************************************/
70 
71  static unsigned char D4DTCH_Init_TSC2046(void);
72  static unsigned char D4DTCH_DeInit_TSC2046(void);
73  static D4D_TOUCHSCREEN_LIMITS* D4DTCH_GetRawLimits_TSC2046(void);
74  static unsigned char D4DTCH_GetPositionRaw_TSC2046(unsigned short *TouchPositionX,
75  unsigned short *TouchPositionY);
76 
77  /**************************************************************/
83  // the main structure that contains low level driver api functions
84  // the name fo this structure is used for recognizing of configured low level driver of whole D4D
85  // so this name has to be used in main configuration header file of D4D driver to enable this driver
86  const D4DTCH_FUNCTIONS d4dtch_tsc2046 =
87  {
88  D4DTCH_Init_TSC2046,
89  D4DTCH_GetPositionRaw_TSC2046,
90  D4DTCH_GetRawLimits_TSC2046,
91  D4DTCH_DeInit_TSC2046
92  };
93 
94  /**************************************************************/
100  /**************************************************************/
107  //-----------------------------------------------------------------------------
108  // FUNCTION: D4DTCH_Init_TSC2046
109  // SCOPE: Low Level Driver API function
110  // DESCRIPTION: The function is used for initialization of this low level driver
111  //
112  // PARAMETERS: none
113  //
114  // RETURNS: result: 1 - Success
115  // 0 - Failed
116  //-----------------------------------------------------------------------------
117  static unsigned char D4DTCH_Init_TSC2046(void)
118  {
119 
120  }
121 
122  //-----------------------------------------------------------------------------
123  // FUNCTION: D4DTCH_DeInit_TSC2046
124  // SCOPE: Low Level Driver API function
125  // DESCRIPTION: The function is used for deinitialization of this low level driver
126  //
127  // PARAMETERS: none
128  //
129  // RETURNS: result: 1 - Success
130  // 0 - Failed
131  //-----------------------------------------------------------------------------
132  static unsigned char D4DTCH_DeInit_TSC2046(void)
133  {
134 
135  }
136 
137  //-----------------------------------------------------------------------------
138  // FUNCTION: D4DTCH_GetRawLimits_TSC2046
139  // SCOPE: Low Level Driver API function
140  // DESCRIPTION: The function returns pointer on touch screen raw limits
141  // structure.
142  // PARAMETERS: none
143  //
144  // RETURNS: pointer on touch screen raw limit structure
145  //
146  //-----------------------------------------------------------------------------
147  static D4D_TOUCHSCREEN_LIMITS* D4DTCH_GetRawLimits_TSC2046(void)
148  {
149 
150  }
151 
152  //-----------------------------------------------------------------------------
153  // FUNCTION: D4DTCH_GetPositionRaw_TSC2046
154  // SCOPE: Low Level Driver API function
155  // DESCRIPTION: Reads touch screen and returns raw uncompensated X, Y
156  // coordinates if screen touched
157  // PARAMETERS: unsigned short *TouchPositionX Pointer to X coordinate
158  // unsigned short *TouchPositionY Pointer to Y ccordinate
159  // RETURNS: 0 no screen touch
160  // 1 screen touch,
161  //-----------------------------------------------------------------------------
162  static unsigned char D4DTCH_GetPositionRaw_TSC2046 (unsigned short *TouchPositionX,
163  unsigned short *TouchPositionY)
164  {
165  (void)TouchPositionX;
166  (void)TouchPositionY;
167 
168 
169  return 0;
170  }
171 
172 #endif //(D4D_MK_STR(D4D_LLD_TCH) == d4dtch_tsc2046_ID)
173 
174 #if 0 // will be implemented ASAP, Petr Gargulak
175 
176 /**************************************************************************
177 *
178 * Copyright 2014 by Petr Gargulak. eGUI Community.
179 * Copyright 2009-2013 by Petr Gargulak. Freescale Semiconductor, Inc.
180 *
181 ***************************************************************************
182 * This program is free software: you can redistribute it and/or modify
183 * it under the terms of the GNU Lesser General Public License Version 3
184 * or later (the "LGPL").
185 *
186 * As a special exception, the copyright holders of the eGUI project give you
187 * permission to link the eGUI sources with independent modules to produce an
188 * executable, regardless of the license terms of these independent modules,
189 * and to copy and distribute the resulting executable under terms of your
190 * choice, provided that you also meet, for each linked independent module,
191 * the terms and conditions of the license of that module.
192 * An independent module is a module which is not derived from or based
193 * on this library.
194 * If you modify the eGUI sources, you may extend this exception
195 * to your version of the eGUI sources, but you are not obligated
196 * to do so. If you do not wish to do so, delete this
197 * exception statement from your version.
198 *
199 * This program is distributed in the hope that it will be useful,
200 * but WITHOUT ANY WARRANTY; without even the implied warranty of
201 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
202 *
203 * You should have received a copy of the GNU General Public License
204 * and the GNU Lesser General Public License along with this program.
205 * If not, see <http://www.gnu.org/licenses/>.
206 *
207 ***************************************************************************/
221 /******************************************************************************
222 * Driver under development
223 ******************************************************************************/
224 
225 
226 #include "d4d.h"
227 
228 #if (D4D_LLD_TCH == D4D_LLD_TCH_TSC2046)
229 
230  #include "d4dtch_TSC2046.h"
231 
232 
233  /******************************************************************************
234  * Macros
235  ******************************************************************************/
236  #define GET(x) (x##_PORT) & (1 << (x))
237  #define SET(x) (x##_PORT) |= (1 << (x))
238  #define RESET(x) (x##_PORT) &= ~(1 << (x))
239 
240  #define OUTPUT(x) (x##_DDR) |= (1 << (x))
241  #define INPUT(x) (x##_DDR) &= ~(1 << (x))
242 
243  #define PULL_UP(x) (x##_PE) |= (1 << (x))
244  #define NO_PULL_UP(x) (x##_PE) &= ~(1 << (x))
245 
246  /**************************************************************/
252  /**************************************************************/
260  /**************************************************************/
266  void D4DTCH_Init(void)
267  {
268 
269  #ifdef D4DTCH_MCU_USER_INIT
270  D4DTCH_MCU_USER_INIT
271  #endif
272 
273  SET(D4DTCH_CS);
274  OUTPUT(D4DTCH_CS);
275 
276  SET(D4DTCH_PENIRQ);
277  INPUT(D4DTCH_PENIRQ);
278 
279  /* Select the highest baud rate prescaler divisor and the highest baud rate divisor */ //AR
280  D4DLCD_SPIBR = 0x34;
281 
282  /* SPI Interrupt disable, system enable and master mode selected */ //AR
283  D4DLCD_SPIC1 = 0x50;
284  D4DLCD_SPIC2 = 0x00;
285 
286  }
287 
288 
289  static unsigned short Tchscr_TSC2046GetChannel(TSC2046_CTRL_BYTE ctrl_byte)
290  {
291 
292  unsigned short tmp_res = 0;
293 
294  (void)D4DLCD_SPIS; // Clear status reg
295  (void)D4DLCD_SPID; // Clear receive status flag
296 
297  RESET(D4DTCH_CS);
298 
299  // Send data byte
300  D4DLCD_SPID = (unsigned char) ctrl_byte.all;
301 
302  while (!D4DLCD_SPIS_SPTEF) {};
303  while (!D4DLCD_SPIS_SPRF) {};
304  (void)D4DLCD_SPID; // Clear receive status flag
305 
306  D4DLCD_SPID = 0;
307 
308  while (!D4DLCD_SPIS_SPTEF) {};
309  while (!D4DLCD_SPIS_SPRF) {};
310 
311  // Send data byte
312  tmp_res = (unsigned short)(D4DLCD_SPID << 8);
313  D4DLCD_SPID = 0;
314 
315  while (!D4DLCD_SPIS_SPTEF) {};
316  while (!D4DLCD_SPIS_SPRF) {};
317  // Send data byte
318  tmp_res |= D4DLCD_SPID;
319 
320 
321  SET(D4DTCH_CS);
322 
323  return (unsigned short)(tmp_res >> 3);
324  }
325 
326  static unsigned char TchScr_GetFilteredAxis(TSC2046_CTRL_BYTE ctrl_byte, unsigned short *Res)
327  {
328  #define AVERAGE_BUFF_LEN 5
329 
330  unsigned short wSample[AVERAGE_BUFF_LEN];
331  unsigned short wRes, wSum;
332  unsigned short wADCReading;
333  unsigned char tmp, tmp_res;
334  int cnt;
335 
336  (void)Tchscr_TSC2046GetChannel(ctrl_byte);
337 
338  for(tmp = 0; tmp < AVERAGE_BUFF_LEN;tmp++) {
339 
340  wSample[tmp] = 0;
341 
342  }
343  wSum = 0;
344  cnt = 0;
345 
346  while (cnt < 33)
347  {
348  cnt++;
349  // Read ADC value
350  wADCReading = Tchscr_TSC2046GetChannel(ctrl_byte);
351 
352  wSum += wADCReading;
353  wSum -= wSample[AVERAGE_BUFF_LEN - 1];
354  for(tmp = (AVERAGE_BUFF_LEN - 1); tmp > 0 ;tmp--)
355  {
356  wSample[tmp] = wSample[tmp - 1];
357  }
358 
359  wSample[0] = wADCReading;
360 
361  wRes = (unsigned short)(wSum / AVERAGE_BUFF_LEN);
362 
363  if(wADCReading < D4DTCH_Y_TOUCH_MIN)
364  return 0;
365 
366  if(cnt > AVERAGE_BUFF_LEN) {
367  tmp_res = 0;
368  for(tmp = 0; tmp < AVERAGE_BUFF_LEN ;tmp++)
369  {
370  if((wRes > (wSample[tmp] - D4DTCH_SAMPLE_MARGIN)) && (wRes < (wSample[tmp] + D4DTCH_SAMPLE_MARGIN)))
371  {
372  tmp_res++;
373  }
374  }
375  if(tmp_res >= (AVERAGE_BUFF_LEN - 2)) {
376  *Res = wRes;
377  return 1;
378  }
379  }
380  }
381  return 0;
382  }
383 
384  //
385  //-----------------------------------------------------------------------------
386  // FUNCTION: LCD_GetTouchScreenPositionRaw
387  // SCOPE: Global
388  // DESCRIPTION: Reads touch screen and returns raw uncompensated X, Y
389  // coordinates if screen touched
390  // PARAMETERS: unsigned int *TouchPositionX Pointer to X coordinate
391  // unsigned int *TouchPositionY Pointer to Y ccordinate
392  // RETURNS: 0 no screen touch
393  // 1 screen touch,
394  //-----------------------------------------------------------------------------
395  //
396  unsigned char D4DTCH_GetPositionRaw (unsigned int *TouchPositionX,
397  unsigned int *TouchPositionY)
398  {
399  // Declare and initialise local variables
400  unsigned short tmpRes;
401  TSC2046_CTRL_BYTE ctrl_byte;
402 
403  if(GET(D4DTCH_PENIRQ)) // Check if touchscreen is touched
404  return 0;
405 
406  ctrl_byte.all = TSC_2046_CTRL_BYTE_DIF12_DEFAULT;
407 
408  ctrl_byte.MergedBits.A = TSC2046_CHNL_DIF_Y;
409  if(!TchScr_GetFilteredAxis(ctrl_byte, &tmpRes))
410  {
411  return 0;
412  }
413 
414  if(tmpRes > D4DTCH_Y_TOUCH_MIN) {
415 
416  #if D4DTCH_AXIS_ORIENT_Y == D4DTCH_AXIS_ORIENT_INVERTED
417  tmpRes = (unsigned short)(D4DTCH_FULL_SCALE - tmpRes);
418  #endif
419 
420  *TouchPositionY = tmpRes;
421  ctrl_byte.MergedBits.A = TSC2046_CHNL_DIF_X;
422  if(!TchScr_GetFilteredAxis(ctrl_byte, &tmpRes)) {
423  return 0;
424  }
425 
426  if(tmpRes > D4DTCH_X_TOUCH_MIN) {
427 
428  #if D4DTCH_AXIS_ORIENT_X == D4DTCH_AXIS_ORIENT_INVERTED
429  tmpRes = (unsigned short)(D4DTCH_FULL_SCALE - tmpRes);
430  #endif
431 
432  *TouchPositionX = tmpRes;
433  return 1;
434  }
435  }
436  return 0;
437  }
438 
439 #endif
440 
441 #endif
#define D4DLCD_SPIS_SPRF
#define D4DLCD_SPIC2
#define INPUT(x)
#define D4DLCD_SPIBR
#define D4DTCH_FULL_SCALE
D4D low level touch screen interface API structure.
Definition: d4d_lldapi.h:195
#define D4DTCH_Y_TOUCH_MIN
#define D4DLCD_SPID
D4D Driver main header file.
#define SET(x)
#define D4DLCD_SPIS
#define RESET(x)
D4D Driver private header file.
#define D4DTCH_SAMPLE_MARGIN
#define OUTPUT(x)
#define D4DLCD_SPIS_SPTEF
D4D low level touch screen limitation structure.
Definition: d4d_lldapi.h:105
#define D4DTCH_X_TOUCH_MIN
D4D driver - tsc2046 touch screen driver function header file.
#define GET(x)
Definition: hw_iic_prv.h:58
D4D driver - resistive touch screen driver function header file.
#define AVERAGE_BUFF_LEN
#define D4DLCD_SPIC1