touch.c 5.2 KB

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