touch.c 11 KB

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