touch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * File : touch.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2010 - 2012, 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. RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
  95. emouse.wid = RT_NULL;
  96. /* set emouse button */
  97. emouse.button = RTGUI_MOUSE_BUTTON_LEFT;
  98. emouse.parent.sender = RT_NULL;
  99. if (updown)
  100. {
  101. ts.xp = ts.xp / ts.count;
  102. ts.yp = ts.yp / ts.count;;
  103. if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
  104. {
  105. touch->x = ts.xp;
  106. touch->y = ts.yp;
  107. }
  108. else
  109. {
  110. if (touch->max_x > touch->min_x)
  111. {
  112. touch->x = touch->width * (ts.xp-touch->min_x)/(touch->max_x-touch->min_x);
  113. }
  114. else
  115. {
  116. touch->x = touch->width * ( touch->min_x - ts.xp ) / (touch->min_x-touch->max_x);
  117. }
  118. if (touch->max_y > touch->min_y)
  119. {
  120. touch->y = touch->height * ( ts.yp - touch->min_y ) / (touch->max_y-touch->min_y);
  121. }
  122. else
  123. {
  124. touch->y = touch->height * ( touch->min_y - ts.yp ) / (touch->min_y-touch->max_y);
  125. }
  126. }
  127. emouse.x = touch->x;
  128. emouse.y = touch->y;
  129. if (touch->first_down_report == RT_TRUE)
  130. {
  131. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  132. emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
  133. }
  134. else
  135. {
  136. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  137. emouse.button = 0;
  138. }
  139. }
  140. else
  141. {
  142. emouse.x = touch->x;
  143. emouse.y = touch->y;
  144. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  145. emouse.button |= RTGUI_MOUSE_BUTTON_UP;
  146. if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
  147. {
  148. /* callback function */
  149. touch->calibration_func(emouse.x, emouse.y);
  150. }
  151. }
  152. /* rt_kprintf("touch %s: ts.x: %d, ts.y: %d\n", updown? "down" : "up",
  153. touch->x, touch->y); */
  154. /* send event to server */
  155. if (touch->calibrating != RT_TRUE)
  156. {
  157. rtgui_server_post_event((&emouse.parent), sizeof(emouse));
  158. }
  159. }
  160. #else
  161. static void report_touch_input(int updown)
  162. {
  163. struct rt_touch_event touch_event;
  164. if (updown)
  165. {
  166. ts.xp = ts.xp / ts.count;
  167. ts.yp = ts.yp / ts.count;
  168. if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
  169. {
  170. touch->x = ts.xp;
  171. touch->y = ts.yp;
  172. }
  173. else
  174. {
  175. if (touch->max_x > touch->min_x)
  176. {
  177. touch->x = touch->width * ( ts.xp - touch->min_x ) / (touch->max_x-touch->min_x);
  178. }
  179. else
  180. {
  181. touch->x = touch->width * ( touch->min_x - ts.xp ) / (touch->min_x-touch->max_x);
  182. }
  183. if (touch->max_y > touch->min_y)
  184. {
  185. touch->y = touch->height * ( ts.yp - touch->min_y ) / (touch->max_y-touch->min_y);
  186. }
  187. else
  188. {
  189. touch->y = touch->height * ( touch->min_y - ts.yp ) / (touch->min_y-touch->max_y);
  190. }
  191. }
  192. touch_event.x = touch->x;
  193. touch_event.y = touch->y;
  194. touch_event.pressed = 1;
  195. if (touch->first_down_report == RT_TRUE)
  196. {
  197. if (touch->calibrating != RT_TRUE && touch->eventpost_func)
  198. {
  199. touch->eventpost_func(touch->eventpost_param, &touch_event);
  200. }
  201. }
  202. }
  203. else
  204. {
  205. touch_event.x = touch->x;
  206. touch_event.y = touch->y;
  207. touch_event.pressed = 0;
  208. if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
  209. {
  210. /* callback function */
  211. touch->calibration_func(touch_event.x, touch_event.y);
  212. }
  213. if (touch->calibrating != RT_TRUE && touch->eventpost_func)
  214. {
  215. touch->eventpost_func(touch->eventpost_param, &touch_event);
  216. }
  217. }
  218. }
  219. #endif
  220. static void touch_timer_fire(void *parameter)
  221. {
  222. rt_uint32_t data0;
  223. rt_uint32_t data1;
  224. int updown;
  225. data0 = ADCDAT0;
  226. data1 = ADCDAT1;
  227. updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
  228. if (updown)
  229. {
  230. if (ts.count != 0)
  231. {
  232. report_touch_input(updown);
  233. }
  234. ts.xp = 0;
  235. ts.yp = 0;
  236. ts.count = 0;
  237. ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
  238. ADCCON |= S3C2410_ADCCON_ENABLE_START;
  239. }
  240. }
  241. static void s3c2410_adc_stylus_action(void)
  242. {
  243. rt_uint32_t data0;
  244. rt_uint32_t data1;
  245. data0 = ADCDAT0;
  246. data1 = ADCDAT1;
  247. ts.xp += data0 & S3C2410_ADCDAT0_XPDATA_MASK;
  248. ts.yp += data1 & S3C2410_ADCDAT1_YPDATA_MASK;
  249. ts.count ++;
  250. if (ts.count < (1<<ts.shift))
  251. {
  252. ADCTSC = S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST;
  253. ADCCON |= S3C2410_ADCCON_ENABLE_START;
  254. }
  255. else
  256. {
  257. if (touch->first_down_report)
  258. {
  259. report_touch_input(1);
  260. ts.xp = 0;
  261. ts.yp = 0;
  262. ts.count = 0;
  263. touch->first_down_report = 0;
  264. }
  265. /* start timer */
  266. rt_timer_start(touch->poll_timer);
  267. ADCTSC = WAIT4INT(1);
  268. }
  269. SUBSRCPND |= BIT_SUB_ADC;
  270. }
  271. static void s3c2410_intc_stylus_updown(void)
  272. {
  273. rt_uint32_t data0;
  274. rt_uint32_t data1;
  275. int updown;
  276. data0 = ADCDAT0;
  277. data1 = ADCDAT1;
  278. updown = (!(data0 & S3C2410_ADCDAT0_UPDOWN)) && (!(data1 & S3C2410_ADCDAT0_UPDOWN));
  279. /* rt_kprintf("stylus: %s\n", updown? "down" : "up"); */
  280. if (updown)
  281. {
  282. touch_timer_fire(0);
  283. }
  284. else
  285. {
  286. /* stop timer */
  287. rt_timer_stop(touch->poll_timer);
  288. touch->first_down_report = RT_TRUE;
  289. if (ts.xp >= 0 && ts.yp >= 0)
  290. {
  291. report_touch_input(updown);
  292. }
  293. ts.count = 0;
  294. ADCTSC = WAIT4INT(0);
  295. }
  296. SUBSRCPND |= BIT_SUB_TC;
  297. }
  298. static void rt_touch_handler(int irqno)
  299. {
  300. if (SUBSRCPND & BIT_SUB_ADC)
  301. {
  302. /* INT_SUB_ADC */
  303. s3c2410_adc_stylus_action();
  304. }
  305. if (SUBSRCPND & BIT_SUB_TC)
  306. {
  307. /* INT_SUB_TC */
  308. s3c2410_intc_stylus_updown();
  309. }
  310. /* clear interrupt */
  311. INTPND |= (1ul << INTADC);
  312. }
  313. /* RT-Thread Device Interface */
  314. static rt_err_t rtgui_touch_init(rt_device_t dev)
  315. {
  316. /* init touch screen structure */
  317. rt_memset(&ts, 0, sizeof(struct s3c2410ts));
  318. ts.delay = 50000;
  319. ts.presc = 9;
  320. ts.shift = 2;
  321. ts.count = 0;
  322. ts.xp = ts.yp = 0;
  323. ADCCON = S3C2410_ADCCON_PRSCEN | S3C2410_ADCCON_PRSCVL(ts.presc);
  324. ADCDLY = ts.delay;
  325. ADCTSC = WAIT4INT(0);
  326. rt_hw_interrupt_install(INTADC, rt_touch_handler, RT_NULL);
  327. rt_hw_interrupt_umask(INTADC);
  328. /* clear interrupt */
  329. INTPND |= (1ul << INTADC);
  330. SUBSRCPND |= BIT_SUB_TC;
  331. SUBSRCPND |= BIT_SUB_ADC;
  332. /* install interrupt handler */
  333. INTSUBMSK &= ~BIT_SUB_ADC;
  334. INTSUBMSK &= ~BIT_SUB_TC;
  335. touch->first_down_report = RT_TRUE;
  336. return RT_EOK;
  337. }
  338. static rt_err_t rtgui_touch_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  339. {
  340. switch (cmd)
  341. {
  342. case RT_TOUCH_CALIBRATION:
  343. touch->calibrating = RT_TRUE;
  344. touch->calibration_func = (rt_touch_calibration_func_t)args;
  345. break;
  346. case RT_TOUCH_NORMAL:
  347. touch->calibrating = RT_FALSE;
  348. break;
  349. case RT_TOUCH_CALIBRATION_DATA:
  350. {
  351. struct calibration_data *data;
  352. data = (struct calibration_data *)args;
  353. /* update */
  354. touch->min_x = data->min_x;
  355. touch->max_x = data->max_x;
  356. touch->min_y = data->min_y;
  357. touch->max_y = data->max_y;
  358. /*
  359. rt_kprintf("min_x = %d, max_x = %d, min_y = %d, max_y = %d\n",
  360. touch->min_x, touch->max_x, touch->min_y, touch->max_y);
  361. */
  362. }
  363. break;
  364. case RT_TOUCH_EVENTPOST:
  365. touch->eventpost_func = (rt_touch_eventpost_func_t)args;
  366. break;
  367. case RT_TOUCH_EVENTPOST_PARAM:
  368. touch->eventpost_param = args;
  369. break;
  370. }
  371. return RT_EOK;
  372. }
  373. void rtgui_touch_hw_init(void)
  374. {
  375. rt_err_t result = RT_FALSE;
  376. rt_device_t device = RT_NULL;
  377. struct rt_device_graphic_info info;
  378. touch = (struct rtgui_touch_device *)rt_malloc(sizeof(struct rtgui_touch_device));
  379. if (touch == RT_NULL)
  380. return; /* no memory yet */
  381. /* clear device structure */
  382. rt_memset(&(touch->parent), 0, sizeof(struct rt_device));
  383. touch->calibrating = RT_FALSE;
  384. touch->min_x = X_MIN;
  385. touch->max_x = X_MAX;
  386. touch->min_y = Y_MIN;
  387. touch->max_y = Y_MAX;
  388. touch->eventpost_func = RT_NULL;
  389. touch->eventpost_param = RT_NULL;
  390. /* init device structure */
  391. touch->parent.type = RT_Device_Class_Unknown;
  392. touch->parent.init = rtgui_touch_init;
  393. touch->parent.control = rtgui_touch_control;
  394. touch->parent.user_data = RT_NULL;
  395. device = rt_device_find("lcd");
  396. if (device == RT_NULL)
  397. return; /* no this device */
  398. /* get graphic device info */
  399. result = rt_device_control(device, RTGRAPHIC_CTRL_GET_INFO, &info);
  400. if (result != RT_EOK)
  401. {
  402. /* get device information failed */
  403. return;
  404. }
  405. touch->width = info.width;
  406. touch->height = info.height;
  407. /* create 1/8 second timer */
  408. touch->poll_timer = rt_timer_create("touch", touch_timer_fire, RT_NULL,
  409. RT_TICK_PER_SECOND/8, RT_TIMER_FLAG_PERIODIC);
  410. /* register touch device to RT-Thread */
  411. rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR);
  412. }