Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

10 лет назад
13 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Copyright 2011,2013 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdbool.h>
  15. #include<avr/io.h>
  16. #include<util/delay.h>
  17. #include "ps2.h"
  18. #include "ps2_mouse.h"
  19. #include "report.h"
  20. #include "host.h"
  21. #include "timer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. static report_mouse_t mouse_report = {};
  25. static void print_usb_data(void);
  26. /* supports only 3 button mouse at this time */
  27. uint8_t ps2_mouse_init(void) {
  28. uint8_t rcv;
  29. ps2_host_init();
  30. _delay_ms(1000); // wait for powering up
  31. // send Reset
  32. rcv = ps2_host_send(0xFF);
  33. print("ps2_mouse_init: send Reset: ");
  34. phex(rcv); phex(ps2_error); print("\n");
  35. // read completion code of BAT
  36. rcv = ps2_host_recv_response();
  37. print("ps2_mouse_init: read BAT: ");
  38. phex(rcv); phex(ps2_error); print("\n");
  39. // read Device ID
  40. rcv = ps2_host_recv_response();
  41. print("ps2_mouse_init: read DevID: ");
  42. phex(rcv); phex(ps2_error); print("\n");
  43. // send Set Remote mode
  44. rcv = ps2_host_send(0xF0);
  45. print("ps2_mouse_init: send 0xF0: ");
  46. phex(rcv); phex(ps2_error); print("\n");
  47. return 0;
  48. }
  49. #define X_IS_NEG (mouse_report.buttons & (1<<PS2_MOUSE_X_SIGN))
  50. #define Y_IS_NEG (mouse_report.buttons & (1<<PS2_MOUSE_Y_SIGN))
  51. #define X_IS_OVF (mouse_report.buttons & (1<<PS2_MOUSE_X_OVFLW))
  52. #define Y_IS_OVF (mouse_report.buttons & (1<<PS2_MOUSE_Y_OVFLW))
  53. void ps2_mouse_task(void)
  54. {
  55. enum { SCROLL_NONE, SCROLL_BTN, SCROLL_SENT };
  56. static uint8_t scroll_state = SCROLL_NONE;
  57. static uint8_t buttons_prev = 0;
  58. /* receives packet from mouse */
  59. uint8_t rcv;
  60. rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
  61. if (rcv == PS2_ACK) {
  62. mouse_report.buttons = ps2_host_recv_response();
  63. mouse_report.x = ps2_host_recv_response();
  64. mouse_report.y = ps2_host_recv_response();
  65. } else {
  66. if (!debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
  67. return;
  68. }
  69. /* if mouse moves or buttons state changes */
  70. if (mouse_report.x || mouse_report.y ||
  71. ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
  72. #ifdef PS2_MOUSE_DEBUG
  73. print("ps2_mouse raw: [");
  74. phex(mouse_report.buttons); print("|");
  75. print_hex8((uint8_t)mouse_report.x); print(" ");
  76. print_hex8((uint8_t)mouse_report.y); print("]\n");
  77. #endif
  78. buttons_prev = mouse_report.buttons;
  79. // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
  80. // bit: 8 7 ... 0
  81. // sign \8-bit/
  82. //
  83. // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
  84. //
  85. // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
  86. mouse_report.x = X_IS_NEG ?
  87. ((!X_IS_OVF && -127 <= mouse_report.x && mouse_report.x <= -1) ? mouse_report.x : -127) :
  88. ((!X_IS_OVF && 0 <= mouse_report.x && mouse_report.x <= 127) ? mouse_report.x : 127);
  89. mouse_report.y = Y_IS_NEG ?
  90. ((!Y_IS_OVF && -127 <= mouse_report.y && mouse_report.y <= -1) ? mouse_report.y : -127) :
  91. ((!Y_IS_OVF && 0 <= mouse_report.y && mouse_report.y <= 127) ? mouse_report.y : 127);
  92. // remove sign and overflow flags
  93. mouse_report.buttons &= PS2_MOUSE_BTN_MASK;
  94. // invert coordinate of y to conform to USB HID mouse
  95. mouse_report.y = -mouse_report.y;
  96. #if PS2_MOUSE_SCROLL_BTN_MASK
  97. static uint16_t scroll_button_time = 0;
  98. if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == (PS2_MOUSE_SCROLL_BTN_MASK)) {
  99. if (scroll_state == SCROLL_NONE) {
  100. scroll_button_time = timer_read();
  101. scroll_state = SCROLL_BTN;
  102. }
  103. // doesn't send Scroll Button
  104. //mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
  105. if (mouse_report.x || mouse_report.y) {
  106. scroll_state = SCROLL_SENT;
  107. mouse_report.v = -mouse_report.y/(PS2_MOUSE_SCROLL_DIVISOR_V);
  108. mouse_report.h = mouse_report.x/(PS2_MOUSE_SCROLL_DIVISOR_H);
  109. mouse_report.x = 0;
  110. mouse_report.y = 0;
  111. //host_mouse_send(&mouse_report);
  112. }
  113. }
  114. else if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == 0) {
  115. #if PS2_MOUSE_SCROLL_BTN_SEND
  116. if (scroll_state == SCROLL_BTN &&
  117. TIMER_DIFF_16(timer_read(), scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
  118. // send Scroll Button(down and up at once) when not scrolled
  119. mouse_report.buttons |= (PS2_MOUSE_SCROLL_BTN_MASK);
  120. host_mouse_send(&mouse_report);
  121. _delay_ms(100);
  122. mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
  123. }
  124. #endif
  125. scroll_state = SCROLL_NONE;
  126. }
  127. // doesn't send Scroll Button
  128. mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
  129. #endif
  130. host_mouse_send(&mouse_report);
  131. print_usb_data();
  132. }
  133. // clear report
  134. mouse_report.x = 0;
  135. mouse_report.y = 0;
  136. mouse_report.v = 0;
  137. mouse_report.h = 0;
  138. mouse_report.buttons = 0;
  139. }
  140. static void print_usb_data(void)
  141. {
  142. if (!debug_mouse) return;
  143. print("ps2_mouse usb: [");
  144. phex(mouse_report.buttons); print("|");
  145. print_hex8((uint8_t)mouse_report.x); print(" ");
  146. print_hex8((uint8_t)mouse_report.y); print(" ");
  147. print_hex8((uint8_t)mouse_report.v); print(" ");
  148. print_hex8((uint8_t)mouse_report.h); print("]\n");
  149. }
  150. /* PS/2 Mouse Synopsis
  151. * http://www.computer-engineering.org/ps2mouse/
  152. *
  153. * Command:
  154. * 0xFF: Reset
  155. * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
  156. * 0xF5: Disable Data Reporting
  157. * 0xF4: Enable Data Reporting
  158. * 0xF3: Set Sample Rate
  159. * 0xF2: Get Device ID
  160. * 0xF0: Set Remote Mode
  161. * 0xEB: Read Data
  162. * 0xEA: Set Stream Mode
  163. * 0xE9: Status Request
  164. * 0xE8: Set Resolution
  165. * 0xE7: Set Scaling 2:1
  166. * 0xE6: Set Scaling 1:1
  167. *
  168. * Mode:
  169. * Stream Mode: devices sends the data when it changs its state
  170. * Remote Mode: host polls the data periodically
  171. *
  172. * This code uses Remote Mode and polls the data with Read Data(0xEB).
  173. *
  174. * Data format:
  175. * byte|7 6 5 4 3 2 1 0
  176. * ----+--------------------------------------------------------------
  177. * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left
  178. * 1| X movement
  179. * 2| Y movement
  180. */