touch.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include <s3c24x0.h>
  4. #include <rtgui/event.h>
  5. #include <rtgui/rtgui_server.h>
  6. /* ADCCON Register Bits */
  7. #define S3C2410_ADCCON_ECFLG (1<<15)
  8. #define S3C2410_ADCCON_PRSCEN (1<<14)
  9. #define S3C2410_ADCCON_PRSCVL(x) (((x)&0xFF)<<6)
  10. #define S3C2410_ADCCON_PRSCVLMASK (0xFF<<6)
  11. #define S3C2410_ADCCON_SELMUX(x) (((x)&0x7)<<3)
  12. #define S3C2410_ADCCON_MUXMASK (0x7<<3)
  13. #define S3C2410_ADCCON_STDBM (1<<2)
  14. #define S3C2410_ADCCON_READ_START (1<<1)
  15. #define S3C2410_ADCCON_ENABLE_START (1<<0)
  16. #define S3C2410_ADCCON_STARTMASK (0x3<<0)
  17. /* ADCTSC Register Bits */
  18. #define S3C2410_ADCTSC_UD_SEN (1<<8) /* ghcstop add for s3c2440a */
  19. #define S3C2410_ADCTSC_YM_SEN (1<<7)
  20. #define S3C2410_ADCTSC_YP_SEN (1<<6)
  21. #define S3C2410_ADCTSC_XM_SEN (1<<5)
  22. #define S3C2410_ADCTSC_XP_SEN (1<<4)
  23. #define S3C2410_ADCTSC_PULL_UP_DISABLE (1<<3)
  24. #define S3C2410_ADCTSC_AUTO_PST (1<<2)
  25. #define S3C2410_ADCTSC_XY_PST(x) (((x)&0x3)<<0)
  26. /* ADCDAT0 Bits */
  27. #define S3C2410_ADCDAT0_UPDOWN (1<<15)
  28. #define S3C2410_ADCDAT0_AUTO_PST (1<<14)
  29. #define S3C2410_ADCDAT0_XY_PST (0x3<<12)
  30. #define S3C2410_ADCDAT0_XPDATA_MASK (0x03FF)
  31. /* ADCDAT1 Bits */
  32. #define S3C2410_ADCDAT1_UPDOWN (1<<15)
  33. #define S3C2410_ADCDAT1_AUTO_PST (1<<14)
  34. #define S3C2410_ADCDAT1_XY_PST (0x3<<12)
  35. #define S3C2410_ADCDAT1_YPDATA_MASK (0x03FF)
  36. #define WAIT4INT(x) (((x)<<8) | \
  37. S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | \
  38. S3C2410_ADCTSC_XY_PST(3))
  39. #define AUTOPST (S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | \
  40. S3C2410_ADCTSC_AUTO_PST | S3C2410_ADCTSC_XY_PST(0))
  41. struct s3c2410ts
  42. {
  43. long xp;
  44. long yp;
  45. int count;
  46. int shift;
  47. int delay;
  48. int presc;
  49. char phys[32];
  50. };
  51. static struct s3c2410ts ts;
  52. #define X_MIN 74
  53. #define X_MAX 934
  54. #define Y_MIN 89
  55. #define Y_MAX 920
  56. #ifdef RT_USING_RTGUI
  57. #include <rtgui/event.h>
  58. void report_touch_input(int updown)
  59. {
  60. long tmp;
  61. struct rtgui_event_mouse emouse;
  62. ts.xp >>= ts.shift;
  63. ts.yp >>= ts.shift;
  64. ts.xp = 240 * (ts.xp-X_MIN)/(X_MAX-X_MIN);
  65. ts.yp = 320 - (320*(ts.yp-Y_MIN)/(Y_MAX-Y_MIN));
  66. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  67. emouse.parent.sender = RT_NULL;
  68. emouse.x = ts.xp;
  69. emouse.y = ts.yp;
  70. /* set emouse button */
  71. if (updown)
  72. {
  73. emouse.button = RTGUI_MOUSE_BUTTON_DOWN;
  74. }
  75. else
  76. {
  77. emouse.button = RTGUI_MOUSE_BUTTON_UP;
  78. }
  79. /* rt_kprintf("touch %s: ts.x: %d, ts.y: %d, count:%d\n", updown? "down" : "up",
  80. ts.xp, ts.yp, ts.count); */
  81. emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
  82. rtgui_server_post_event((&emouse.parent), sizeof(emouse));
  83. }
  84. #endif
  85. static int first_down_report;
  86. /* fixed me, use timer to support move action */
  87. static void touch_timer_fire(void)
  88. {
  89. if (ts.count == 3)
  90. {
  91. if (first_down_report)
  92. {
  93. #ifdef RT_USING_RTGUI
  94. report_touch_input(1);
  95. first_down_report = 0;
  96. #endif
  97. }
  98. }
  99. ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
  100. ADCCON |= S3C2410_ADCCON_ENABLE_START;
  101. }
  102. void s3c2410_adc_stylus_action()
  103. {
  104. rt_uint32_t data0;
  105. rt_uint32_t data1;
  106. SUBSRCPND |= BIT_SUB_ADC;
  107. data0 = ADCDAT0;
  108. data1 = ADCDAT1;
  109. ts.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
  110. ts.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
  111. ts.count++;
  112. if (ts.count < (1<<ts.shift))
  113. {
  114. ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
  115. ADCCON |= S3C2410_ADCCON_ENABLE_START;
  116. }
  117. else
  118. {
  119. //touch_timer_fire();
  120. ADCTSC = WAIT4INT(1);
  121. }
  122. }
  123. void s3c2410_intc_stylus_updown()
  124. {
  125. rt_uint32_t data0;
  126. rt_uint32_t data1;
  127. int updown;
  128. SUBSRCPND |= BIT_SUB_TC;
  129. data0 = ADCDAT0;
  130. data1 = ADCDAT1;
  131. updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
  132. if (updown)
  133. {
  134. touch_timer_fire();
  135. }
  136. else
  137. {
  138. if (ts.xp >= 0 && ts.yp >= 0)
  139. {
  140. #ifdef RT_USING_RTGUI
  141. report_touch_input(0);
  142. first_down_report = 1;
  143. #endif
  144. }
  145. ts.xp = 0;
  146. ts.yp = 0;
  147. ts.count = 0;
  148. ADCTSC = WAIT4INT(0);
  149. }
  150. }
  151. void rt_touch_handler(int irqno)
  152. {
  153. if (SUBSRCPND & (1 << 10))
  154. {
  155. /* INT_SUB_ADC */
  156. s3c2410_adc_stylus_action();
  157. }
  158. if (SUBSRCPND & (1 << 9))
  159. {
  160. /* INT_SUB_TC */
  161. s3c2410_intc_stylus_updown();
  162. }
  163. /* clear interrupt */
  164. INTPND |= (rt_uint32_t)(1 << INTADC);
  165. }
  166. void rt_hw_touch_init()
  167. {
  168. /* init touch screen structure */
  169. rt_memset(&ts, 0, sizeof(struct s3c2410ts));
  170. ts.delay = 50000;
  171. ts.presc = 9;
  172. ts.shift = 2;
  173. ts.count = 0;
  174. ts.xp = ts.yp = 0;
  175. ADCCON = S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(ts.presc);
  176. ADCDLY = ts.delay;
  177. ADCTSC = WAIT4INT(0);
  178. rt_hw_interrupt_install(INTADC, rt_touch_handler, RT_NULL);
  179. rt_hw_interrupt_umask(INTADC);
  180. /* clear interrupt */
  181. INTPND |= (rt_uint32_t)(1 << INTADC);
  182. SUBSRCPND |= BIT_SUB_TC;
  183. SUBSRCPND |= BIT_SUB_ADC;
  184. /* install interrupt handler */
  185. INTSUBMSK &= ~BIT_SUB_ADC;
  186. INTSUBMSK &= ~BIT_SUB_TC;
  187. first_down_report = 1;
  188. }