touch.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 65
  55. #define Y_MAX 933
  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. tmp = ts.xp;
  63. ts.xp = ts.yp;
  64. ts.yp = tmp;
  65. ts.xp >>= ts.shift;
  66. ts.yp >>= ts.shift;
  67. ts.xp = 240 * (ts.xp-X_MIN)/(X_MAX-X_MIN);
  68. ts.yp = 320 - (320*(ts.yp-Y_MIN)/(Y_MAX-Y_MIN));
  69. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  70. emouse.parent.sender = RT_NULL;
  71. emouse.x = ts.xp;
  72. emouse.y = ts.yp;
  73. /* set emouse button */
  74. if (updown)
  75. {
  76. emouse.button = RTGUI_MOUSE_BUTTON_DOWN;
  77. }
  78. else
  79. {
  80. emouse.button = RTGUI_MOUSE_BUTTON_UP;
  81. }
  82. rt_kprintf("touch %s: ts.x: %d, ts.y: %d, count:%d\n", updown? "down" : "up",
  83. ts.xp, ts.yp, ts.count);
  84. emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
  85. rtgui_server_post_event((&emouse.parent), sizeof(emouse));
  86. }
  87. #endif
  88. static int first_down_report;
  89. static void touch_timer_fire(void* parameter)
  90. {
  91. rt_uint32_t data0;
  92. rt_uint32_t data1;
  93. int updown;
  94. data0 = ADCDAT0;
  95. data1 = ADCDAT1;
  96. updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
  97. if (updown)
  98. {
  99. if (ts.count != 0)
  100. {
  101. // if (first_down_report)
  102. {
  103. #ifdef RT_USING_RTGUI
  104. report_touch_input(updown);
  105. first_down_report = 0;
  106. #endif
  107. }
  108. }
  109. ts.xp = 0;
  110. ts.yp = 0;
  111. ts.count = 0;
  112. ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
  113. ADCCON |= S3C2410_ADCCON_ENABLE_START;
  114. }
  115. else
  116. {
  117. // if (ts.xp >= 0 && ts.yp >= 0)
  118. {
  119. #ifdef RT_USING_RTGUI
  120. report_touch_input(updown);
  121. first_down_report = 1;
  122. #endif
  123. }
  124. ts.count = 0;
  125. rt_kprintf("touch time fire: up\n");
  126. ADCTSC = WAIT4INT(0);
  127. }
  128. }
  129. void s3c2410_adc_stylus_action()
  130. {
  131. rt_uint32_t data0;
  132. rt_uint32_t data1;
  133. data0 = ADCDAT0;
  134. data1 = ADCDAT1;
  135. ts.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
  136. ts.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
  137. ts.count++;
  138. if (ts.count < (1<<ts.shift))
  139. {
  140. ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
  141. ADCCON |= S3C2410_ADCCON_ENABLE_START;
  142. }
  143. else
  144. {
  145. touch_timer_fire(0);
  146. ADCTSC = WAIT4INT(1);
  147. }
  148. SUBSRCPND |= BIT_SUB_ADC;
  149. }
  150. void s3c2410_intc_stylus_updown()
  151. {
  152. rt_uint32_t data0;
  153. rt_uint32_t data1;
  154. int updown;
  155. data0 = ADCDAT0;
  156. data1 = ADCDAT1;
  157. updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
  158. rt_kprintf("stylus: %s\n", updown? "down" : "up");
  159. if (updown) touch_timer_fire(RT_NULL);
  160. else
  161. {
  162. if (ts.xp >= 0 && ts.yp >= 0)
  163. {
  164. #ifdef RT_USING_RTGUI
  165. report_touch_input(updown);
  166. first_down_report = 1;
  167. #endif
  168. }
  169. ADCTSC = WAIT4INT(0);
  170. }
  171. SUBSRCPND |= BIT_SUB_TC;
  172. }
  173. void rt_touch_handler(int irqno)
  174. {
  175. if (SUBSRCPND & (1 << 10))
  176. {
  177. /* INT_SUB_ADC */
  178. s3c2410_adc_stylus_action();
  179. }
  180. if (SUBSRCPND & (1 << 9))
  181. {
  182. /* INT_SUB_TC */
  183. s3c2410_intc_stylus_updown();
  184. }
  185. /* clear interrupt */
  186. INTPND |= (rt_uint32_t)(1 << INTADC);
  187. }
  188. void rt_hw_touch_init()
  189. {
  190. /* init touch screen structure */
  191. rt_memset(&ts, 0, sizeof(struct s3c2410ts));
  192. ts.delay = 5000;
  193. ts.presc = 49;
  194. ts.shift = 2;
  195. ADCCON = S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(ts.presc);
  196. ADCDLY = ts.delay;
  197. ADCTSC = WAIT4INT(0);
  198. rt_hw_interrupt_install(INTADC, rt_touch_handler, RT_NULL);
  199. rt_hw_interrupt_umask(INTADC);
  200. /* install interrupt handler */
  201. INTSUBMSK &= ~BIT_SUB_ADC;
  202. INTSUBMSK &= ~BIT_SUB_TC;
  203. first_down_report = 1;
  204. }