touch.c 10 KB

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