touch.c 13 KB

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