drv_mouse.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020/12/31 Bernard Add license info
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #include "interrupt.h"
  15. #include "drv_mouse.h"
  16. #include "drv_clcd.h"
  17. #define DBG_TAG "drv.mouse"
  18. #define DBG_LVL DBG_INFO
  19. #include "rtdbg.h"
  20. #define MOUSE_ADDRESS (0x10007000)
  21. #define MOUSE_IRQ_NUM (IRQ_VEXPRESS_A9_MOUSE)
  22. #define MOUSE_XMAX (BSP_LCD_WIDTH)
  23. #define MOUSE_YMAX (BSP_LCD_HEIGHT)
  24. #define MOUSE_BUTTON_LEFT (0x01)
  25. #define MOUSE_BUTTON_RIGHT (0x02)
  26. #define MOUSE_BUTTON_MIDDLE (0x04)
  27. #define MOUSE_BUTTON_DOWN (0x10)
  28. #define MOUSE_BUTTON_UP (0x20)
  29. #define MOUSE_BUTTON_MOVE (0x40)
  30. #define MOUSE_BUTTON_WHELL (0x80)
  31. #ifdef PKG_USING_GUIENGINE
  32. #include <rtgui/event.h>
  33. #include <rtgui/rtgui_server.h>
  34. typedef rt_uint32_t virtual_addr_t;
  35. enum {
  36. MOUSE_CR = 0x00,
  37. MOUSE_STAT = 0x04,
  38. MOUSE_DATA = 0x08,
  39. MOUSE_CLKDIV = 0x0c,
  40. MOUSE_IIR = 0x10,
  41. };
  42. struct mouse_pl050_pdata_t {
  43. virtual_addr_t virt;
  44. int irq;
  45. int xmax, ymax;
  46. int xpos, ypos;
  47. unsigned char packet[4];
  48. int index;
  49. int obtn;
  50. int type;
  51. };
  52. rt_inline rt_uint8_t read8(uint32_t addr)
  53. {
  54. return (*((volatile rt_uint8_t *)(addr)));
  55. }
  56. rt_inline void write8(uint32_t addr, rt_uint8_t value)
  57. {
  58. *((volatile rt_uint8_t *)(addr)) = value;
  59. }
  60. rt_inline rt_uint32_t read32(uint32_t addr)
  61. {
  62. return (*((volatile rt_uint32_t *)(addr)));
  63. }
  64. rt_inline void write32(uint32_t addr, rt_uint32_t value)
  65. {
  66. *((volatile rt_uint32_t *)(addr)) = value;
  67. }
  68. rt_inline int kmi_write(struct mouse_pl050_pdata_t * pdat, rt_uint8_t value)
  69. {
  70. int timeout = 1000;
  71. while((read8(pdat->virt + MOUSE_STAT) & (1 << 6)) == 0 && timeout--);
  72. if(timeout)
  73. {
  74. write8(pdat->virt + MOUSE_DATA, value);
  75. while((read8(pdat->virt + MOUSE_STAT) & (1 << 4)) == 0);
  76. if(read8(pdat->virt + MOUSE_DATA) == 0xfa)
  77. return RT_TRUE;
  78. }
  79. return RT_FALSE;
  80. }
  81. rt_inline int kmi_read(struct mouse_pl050_pdata_t * pdat, rt_uint8_t * value)
  82. {
  83. if((read8(pdat->virt + MOUSE_STAT) & (1 << 4)))
  84. {
  85. *value = read8(pdat->virt + MOUSE_DATA);
  86. return RT_TRUE;
  87. }
  88. return RT_FALSE;
  89. }
  90. static rt_uint32_t emouse_id;
  91. void push_event_touch_move(int x, int y)
  92. {
  93. struct rtgui_event_mouse emouse;
  94. emouse.parent.sender = RT_NULL;
  95. emouse.wid = RT_NULL;
  96. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  97. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  98. emouse.x = x;
  99. emouse.y = y;
  100. emouse.ts = rt_tick_get();
  101. emouse.id = emouse_id;
  102. LOG_D("[line]:%d motion event id:%d x:%d y:%d", __LINE__, emouse.id, x, y);
  103. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  104. }
  105. void push_event_touch_begin(int x, int y)
  106. {
  107. struct rtgui_event_mouse emouse;
  108. emouse_id = rt_tick_get();
  109. emouse.parent.sender = RT_NULL;
  110. emouse.wid = RT_NULL;
  111. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  112. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  113. emouse.x = x;
  114. emouse.y = y;
  115. emouse.ts = rt_tick_get();
  116. emouse.id = emouse_id;
  117. LOG_D("[line]:%d down event id:%d x:%d y:%d", __LINE__, emouse.id, x, y);
  118. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  119. }
  120. void push_event_touch_end(int x, int y)
  121. {
  122. struct rtgui_event_mouse emouse;
  123. emouse.parent.sender = RT_NULL;
  124. emouse.wid = RT_NULL;
  125. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  126. emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
  127. emouse.x = x;
  128. emouse.y = y;
  129. emouse.ts = rt_tick_get();
  130. emouse.id = emouse_id;
  131. LOG_D("[line]:%d up event id:%d x:%d y:%d", __LINE__, emouse.id, x, y);
  132. rtgui_server_post_event(&emouse.parent, sizeof(emouse));
  133. }
  134. static void mouse_pl050_interrupt(int irq, void * data)
  135. {
  136. struct mouse_pl050_pdata_t * pdat = (struct mouse_pl050_pdata_t *)data;
  137. int x, y, relx, rely, delta;
  138. int btndown, btnup, btn;
  139. int status = 0;
  140. status = read8(pdat->virt + MOUSE_IIR);
  141. while(status & (1 << 0))
  142. {
  143. pdat->packet[pdat->index] = read8(pdat->virt + MOUSE_DATA);
  144. pdat->index = (pdat->index + 1) & 0x3;
  145. if(pdat->index == 0)
  146. {
  147. btn = pdat->packet[0] & 0x7;
  148. btndown = (btn ^ pdat->obtn) & btn;
  149. btnup = (btn ^ pdat->obtn) & pdat->obtn;
  150. pdat->obtn = btn;
  151. if(pdat->packet[0] & 0x10)
  152. relx = 0xffffff00 | pdat->packet[1];
  153. else
  154. relx = pdat->packet[1];
  155. if(pdat->packet[0] & 0x20)
  156. rely = 0xffffff00 | pdat->packet[2];
  157. else
  158. rely = pdat->packet[2];
  159. rely = -rely;
  160. delta = pdat->packet[3] & 0xf;
  161. if(delta == 0xf)
  162. delta = -1;
  163. if(relx != 0)
  164. {
  165. pdat->xpos = pdat->xpos + relx;
  166. if(pdat->xpos < 0)
  167. pdat->xpos = 0;
  168. if(pdat->xpos > pdat->xmax - 1)
  169. pdat->xpos = pdat->xmax - 1;
  170. }
  171. if(rely != 0)
  172. {
  173. pdat->ypos = pdat->ypos + rely;
  174. if(pdat->ypos < 0)
  175. pdat->ypos = 0;
  176. if(pdat->ypos > pdat->ymax - 1)
  177. pdat->ypos = pdat->ymax - 1;
  178. }
  179. x = pdat->xpos;
  180. y = pdat->ypos;
  181. if((btn & (0x01 << 0)) && ((relx != 0) || (rely != 0)))
  182. push_event_touch_move(x, y);
  183. if(btndown & (0x01 << 0))
  184. push_event_touch_begin(x, y);
  185. if(btnup & (0x01 << 0))
  186. push_event_touch_end(x, y);
  187. }
  188. status = read8(pdat->virt + MOUSE_IIR);
  189. }
  190. }
  191. int rt_hw_mouse_init(void)
  192. {
  193. rt_uint8_t value;
  194. rt_uint32_t id;
  195. struct mouse_pl050_pdata_t *pdat;
  196. virtual_addr_t virt = MOUSE_ADDRESS;
  197. int irq = MOUSE_IRQ_NUM;
  198. id = (((read32(virt + 0xfec) & 0xff) << 24) |
  199. ((read32(virt + 0xfe8) & 0xff) << 16) |
  200. ((read32(virt + 0xfe4) & 0xff) << 8) |
  201. ((read32(virt + 0xfe0) & 0xff) << 0));
  202. if(((id >> 12) & 0xff) != 0x41 || (id & 0xfff) != 0x050)
  203. {
  204. LOG_E("read id fail id:0x%08x", id);
  205. return RT_ERROR;
  206. }
  207. pdat = rt_malloc(sizeof(struct mouse_pl050_pdata_t));
  208. if(!pdat)
  209. {
  210. LOG_E("malloc memory failed");
  211. return RT_ERROR;
  212. }
  213. rt_memset(pdat, 0, sizeof(struct mouse_pl050_pdata_t));
  214. pdat->virt = virt;
  215. pdat->irq = irq;
  216. pdat->xmax = MOUSE_XMAX;
  217. pdat->ymax = MOUSE_YMAX;
  218. pdat->xpos = pdat->xmax / 2;
  219. pdat->ypos = pdat->ymax / 2;
  220. pdat->packet[0] = 0;
  221. pdat->packet[1] = 0;
  222. pdat->packet[2] = 0;
  223. pdat->packet[3] = 0;
  224. pdat->index = 0;
  225. pdat->obtn = 0;
  226. write8(pdat->virt + MOUSE_CLKDIV, 0);
  227. write8(pdat->virt + MOUSE_CR, (1 << 2));
  228. kmi_write(pdat, 0xff);
  229. kmi_read(pdat, &value);
  230. kmi_write(pdat, 0xf3);
  231. kmi_write(pdat, 200);
  232. kmi_write(pdat, 0xf3);
  233. kmi_write(pdat, 100);
  234. kmi_write(pdat, 0xf3);
  235. kmi_write(pdat, 80);
  236. kmi_write(pdat, 0xf2);
  237. kmi_read(pdat, &value);
  238. kmi_read(pdat, &value);
  239. kmi_write(pdat, 0xf3);
  240. kmi_write(pdat, 100);
  241. kmi_write(pdat, 0xe8);
  242. kmi_write(pdat, 0x02);
  243. kmi_write(pdat, 0xe6);
  244. kmi_write(pdat, 0xf4);
  245. kmi_read(pdat, &value);
  246. kmi_read(pdat, &value);
  247. kmi_read(pdat, &value);
  248. kmi_read(pdat, &value);
  249. write8(pdat->virt + MOUSE_CR, (1 << 2) | (1 << 4));
  250. rt_hw_interrupt_install(pdat->irq, mouse_pl050_interrupt, (void *)pdat, "mouse");
  251. rt_hw_interrupt_umask(pdat->irq);
  252. return RT_EOK;
  253. }
  254. INIT_DEVICE_EXPORT(rt_hw_mouse_init);
  255. #endif