touch.c 12 KB

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