touch.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include <s3c24x0.h>
  4. #include <rtgui/rtgui_system.h>
  5. #include <rtgui/rtgui_server.h>
  6. #include <rtgui/event.h>
  7. #include "touch.h"
  8. /* ADCCON Register Bits */
  9. #define S3C2410_ADCCON_ECFLG (1<<15)
  10. #define S3C2410_ADCCON_PRSCEN (1<<14)
  11. #define S3C2410_ADCCON_PRSCVL(x) (((x)&0xFF)<<6)
  12. #define S3C2410_ADCCON_PRSCVLMASK (0xFF<<6)
  13. #define S3C2410_ADCCON_SELMUX(x) (((x)&0x7)<<3)
  14. #define S3C2410_ADCCON_MUXMASK (0x7<<3)
  15. #define S3C2410_ADCCON_STDBM (1<<2)
  16. #define S3C2410_ADCCON_READ_START (1<<1)
  17. #define S3C2410_ADCCON_ENABLE_START (1<<0)
  18. #define S3C2410_ADCCON_STARTMASK (0x3<<0)
  19. /* ADCTSC Register Bits */
  20. #define S3C2410_ADCTSC_UD_SEN (1<<8) /* ghcstop add for s3c2440a */
  21. #define S3C2410_ADCTSC_YM_SEN (1<<7)
  22. #define S3C2410_ADCTSC_YP_SEN (1<<6)
  23. #define S3C2410_ADCTSC_XM_SEN (1<<5)
  24. #define S3C2410_ADCTSC_XP_SEN (1<<4)
  25. #define S3C2410_ADCTSC_PULL_UP_DISABLE (1<<3)
  26. #define S3C2410_ADCTSC_AUTO_PST (1<<2)
  27. #define S3C2410_ADCTSC_XY_PST(x) (((x)&0x3)<<0)
  28. /* ADCDAT0 Bits */
  29. #define S3C2410_ADCDAT0_UPDOWN (1<<15)
  30. #define S3C2410_ADCDAT0_AUTO_PST (1<<14)
  31. #define S3C2410_ADCDAT0_XY_PST (0x3<<12)
  32. #define S3C2410_ADCDAT0_XPDATA_MASK (0x03FF)
  33. /* ADCDAT1 Bits */
  34. #define S3C2410_ADCDAT1_UPDOWN (1<<15)
  35. #define S3C2410_ADCDAT1_AUTO_PST (1<<14)
  36. #define S3C2410_ADCDAT1_XY_PST (0x3<<12)
  37. #define S3C2410_ADCDAT1_YPDATA_MASK (0x03FF)
  38. #define WAIT4INT(x) (((x)<<8) | \
  39. S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | \
  40. S3C2410_ADCTSC_XY_PST(3))
  41. #define AUTOPST (S3C2410_ADCTSC_YM_SEN | S3C2410_ADCTSC_YP_SEN | S3C2410_ADCTSC_XP_SEN | \
  42. S3C2410_ADCTSC_AUTO_PST | S3C2410_ADCTSC_XY_PST(0))
  43. #define X_MIN 74
  44. #define X_MAX 934
  45. #define Y_MIN 89
  46. #define Y_MAX 920
  47. struct s3c2410ts
  48. {
  49. long xp;
  50. long yp;
  51. int count;
  52. int shift;
  53. int delay;
  54. int presc;
  55. char phys[32];
  56. };
  57. static struct s3c2410ts ts;
  58. struct rtgui_touch_device
  59. {
  60. struct rt_device parent;
  61. rt_timer_t poll_timer;
  62. rt_uint16_t x, y;
  63. rt_bool_t calibrating;
  64. rt_touch_calibration_func_t calibration_func;
  65. rt_uint16_t min_x, max_x;
  66. rt_uint16_t min_y, max_y;
  67. };
  68. static struct rtgui_touch_device *touch = RT_NULL;
  69. static int first_down_report;
  70. static void report_touch_input(int updown)
  71. {
  72. struct rtgui_event_mouse emouse;
  73. /* set emouse button */
  74. emouse.button = RTGUI_MOUSE_BUTTON_LEFT;
  75. emouse.parent.sender = RT_NULL;
  76. if (updown)
  77. {
  78. ts.xp = ts.xp / ts.count;
  79. ts.yp = ts.yp / ts.count;;
  80. if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
  81. {
  82. touch->x = ts.xp;
  83. touch->y = ts.yp;
  84. }
  85. else
  86. {
  87. touch->x = 240 * (ts.xp-touch->min_x)/(touch->max_x-touch->min_x);
  88. touch->y = 320 - (320*(ts.yp-touch->min_y)/(touch->max_y-touch->min_y));
  89. }
  90. emouse.x = touch->x;
  91. emouse.y = touch->y;
  92. if(first_down_report == 1)
  93. {
  94. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  95. emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
  96. }
  97. else
  98. {
  99. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  100. emouse.button = 0;
  101. }
  102. }
  103. else
  104. {
  105. emouse.x = touch->x;
  106. emouse.y = touch->y;
  107. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  108. emouse.button |= RTGUI_MOUSE_BUTTON_UP;
  109. if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
  110. {
  111. /* callback function */
  112. touch->calibration_func(emouse.x, emouse.y);
  113. }
  114. }
  115. /*
  116. rt_kprintf("touch %s: ts.x: %d, ts.y: %d\n", updown? "down" : "up",
  117. touch->x, touch->y);
  118. */
  119. /* send event to server */
  120. if (touch->calibrating != RT_TRUE)
  121. {
  122. rtgui_server_post_event((&emouse.parent), sizeof(emouse));
  123. }
  124. }
  125. static void touch_timer_fire(void* parameter)
  126. {
  127. rt_uint32_t data0;
  128. rt_uint32_t data1;
  129. int updown;
  130. data0 = ADCDAT0;
  131. data1 = ADCDAT1;
  132. updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
  133. if (updown)
  134. {
  135. if (ts.count != 0)
  136. {
  137. report_touch_input(updown);
  138. }
  139. ts.xp = 0;
  140. ts.yp = 0;
  141. ts.count = 0;
  142. ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
  143. ADCCON |= S3C2410_ADCCON_ENABLE_START;
  144. }
  145. }
  146. static void s3c2410_adc_stylus_action(void)
  147. {
  148. rt_uint32_t data0;
  149. rt_uint32_t data1;
  150. data0 = ADCDAT0;
  151. data1 = ADCDAT1;
  152. ts.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
  153. ts.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
  154. ts.count++;
  155. if (ts.count < (1<<ts.shift))
  156. {
  157. ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
  158. ADCCON |= S3C2410_ADCCON_ENABLE_START;
  159. }
  160. else
  161. {
  162. if (first_down_report)
  163. {
  164. report_touch_input(1);
  165. ts.xp = 0;
  166. ts.yp = 0;
  167. ts.count = 0;
  168. first_down_report = 0;
  169. }
  170. /* start timer */
  171. rt_timer_start(touch->poll_timer);
  172. ADCTSC = WAIT4INT(1);
  173. }
  174. SUBSRCPND |= BIT_SUB_ADC;
  175. }
  176. static void s3c2410_intc_stylus_updown(void)
  177. {
  178. rt_uint32_t data0;
  179. rt_uint32_t data1;
  180. int updown;
  181. data0 = ADCDAT0;
  182. data1 = ADCDAT1;
  183. updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
  184. /* rt_kprintf("stylus: %s\n", updown? "down" : "up"); */
  185. if (updown)
  186. {
  187. touch_timer_fire(0);
  188. }
  189. else
  190. {
  191. /* stop timer */
  192. rt_timer_stop(touch->poll_timer);
  193. first_down_report = 1;
  194. if (ts.xp >= 0 && ts.yp >= 0)
  195. {
  196. report_touch_input(updown);
  197. }
  198. ts.count = 0;
  199. ADCTSC = WAIT4INT(0);
  200. }
  201. SUBSRCPND |= BIT_SUB_TC;
  202. }
  203. static void rt_touch_handler(int irqno)
  204. {
  205. if (SUBSRCPND & BIT_SUB_ADC)
  206. {
  207. /* INT_SUB_ADC */
  208. s3c2410_adc_stylus_action();
  209. }
  210. if (SUBSRCPND & BIT_SUB_TC)
  211. {
  212. /* INT_SUB_TC */
  213. s3c2410_intc_stylus_updown();
  214. }
  215. /* clear interrupt */
  216. INTPND |= (rt_uint32_t)(1 << INTADC);
  217. }
  218. /* RT-Thread Device Interface */
  219. static rt_err_t rtgui_touch_init (rt_device_t dev)
  220. {
  221. /* init touch screen structure */
  222. rt_memset(&ts, 0, sizeof(struct s3c2410ts));
  223. ts.delay = 50000;
  224. ts.presc = 9;
  225. ts.shift = 2;
  226. ts.count = 0;
  227. ts.xp = ts.yp = 0;
  228. ADCCON = S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(ts.presc);
  229. ADCDLY = ts.delay;
  230. ADCTSC = WAIT4INT(0);
  231. rt_hw_interrupt_install(INTADC, rt_touch_handler, RT_NULL);
  232. rt_hw_interrupt_umask(INTADC);
  233. /* clear interrupt */
  234. INTPND |= (rt_uint32_t)(1 << INTADC);
  235. SUBSRCPND |= BIT_SUB_TC;
  236. SUBSRCPND |= BIT_SUB_ADC;
  237. /* install interrupt handler */
  238. INTSUBMSK &= ~BIT_SUB_ADC;
  239. INTSUBMSK &= ~BIT_SUB_TC;
  240. first_down_report = 1;
  241. return RT_EOK;
  242. }
  243. static rt_err_t rtgui_touch_control (rt_device_t dev, rt_uint8_t cmd, void *args)
  244. {
  245. switch (cmd)
  246. {
  247. case RT_TOUCH_CALIBRATION:
  248. touch->calibrating = RT_TRUE;
  249. touch->calibration_func = (rt_touch_calibration_func_t)args;
  250. break;
  251. case RT_TOUCH_NORMAL:
  252. touch->calibrating = RT_FALSE;
  253. break;
  254. case RT_TOUCH_CALIBRATION_DATA:
  255. {
  256. struct calibration_data* data;
  257. data = (struct calibration_data*) args;
  258. //update
  259. touch->min_x = data->min_x;
  260. touch->max_x = data->max_x;
  261. touch->min_y = data->max_y;
  262. touch->max_y = data->min_y;
  263. /*
  264. rt_kprintf("min_x = %d, max_x = %d, min_y = %d, max_y = %d\n",
  265. touch->min_x, touch->max_x, touch->min_y, touch->max_y);
  266. */
  267. }
  268. break;
  269. }
  270. return RT_EOK;
  271. }
  272. void rtgui_touch_hw_init(void)
  273. {
  274. touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device));
  275. if (touch == RT_NULL) return; /* no memory yet */
  276. /* clear device structure */
  277. rt_memset(&(touch->parent), 0, sizeof(struct rt_device));
  278. touch->calibrating = RT_FALSE;
  279. touch->min_x = X_MIN;
  280. touch->max_x = X_MAX;
  281. touch->min_y = Y_MIN;
  282. touch->max_y = X_MAX;
  283. /* init device structure */
  284. touch->parent.type = RT_Device_Class_Unknown;
  285. touch->parent.init = rtgui_touch_init;
  286. touch->parent.control = rtgui_touch_control;
  287. touch->parent.private = RT_NULL;
  288. /* create 1/8 second timer */
  289. touch->poll_timer = rt_timer_create("touch", touch_timer_fire, RT_NULL,
  290. RT_TICK_PER_SECOND/8, RT_TIMER_FLAG_PERIODIC);
  291. /* register touch device to RT-Thread */
  292. rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR);
  293. }