drv_mouse.c 7.5 KB

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