touch.c 13 KB

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