1
0

drv_mouse.c 7.5 KB

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