touch.c 9.5 KB

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