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

10 лет назад
13 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. xprintf("%ud ", timer_read());
  70. print("ps2_mouse raw: [");
  71. phex(mouse_report.buttons); print("|");
  72. print_hex8((uint8_t)mouse_report.x); print(" ");
  73. print_hex8((uint8_t)mouse_report.y); print("]\n");
  74. /* if mouse moves or buttons state changes */
  75. if (mouse_report.x || mouse_report.y ||
  76. ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
  77. #ifdef PS2_MOUSE_DEBUG
  78. print("ps2_mouse raw: [");
  79. phex(mouse_report.buttons); print("|");
  80. print_hex8((uint8_t)mouse_report.x); print(" ");
  81. print_hex8((uint8_t)mouse_report.y); print("]\n");
  82. #endif
  83. buttons_prev = mouse_report.buttons;
  84. // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
  85. // bit: 8 7 ... 0
  86. // sign \8-bit/
  87. //
  88. // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
  89. //
  90. // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
  91. mouse_report.x = X_IS_NEG ?
  92. ((!X_IS_OVF && -127 <= mouse_report.x && mouse_report.x <= -1) ? mouse_report.x : -127) :
  93. ((!X_IS_OVF && 0 <= mouse_report.x && mouse_report.x <= 127) ? mouse_report.x : 127);
  94. mouse_report.y = Y_IS_NEG ?
  95. ((!Y_IS_OVF && -127 <= mouse_report.y && mouse_report.y <= -1) ? mouse_report.y : -127) :
  96. ((!Y_IS_OVF && 0 <= mouse_report.y && mouse_report.y <= 127) ? mouse_report.y : 127);
  97. // remove sign and overflow flags
  98. mouse_report.buttons &= PS2_MOUSE_BTN_MASK;
  99. // invert coordinate of y to conform to USB HID mouse
  100. mouse_report.y = -mouse_report.y;
  101. #if PS2_MOUSE_SCROLL_BTN_MASK
  102. static uint16_t scroll_button_time = 0;
  103. if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == (PS2_MOUSE_SCROLL_BTN_MASK)) {
  104. if (scroll_state == SCROLL_NONE) {
  105. scroll_button_time = timer_read();
  106. scroll_state = SCROLL_BTN;
  107. }
  108. // doesn't send Scroll Button
  109. //mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
  110. if (mouse_report.x || mouse_report.y) {
  111. scroll_state = SCROLL_SENT;
  112. mouse_report.v = -mouse_report.y/(PS2_MOUSE_SCROLL_DIVISOR_V);
  113. mouse_report.h = mouse_report.x/(PS2_MOUSE_SCROLL_DIVISOR_H);
  114. mouse_report.x = 0;
  115. mouse_report.y = 0;
  116. //host_mouse_send(&mouse_report);
  117. }
  118. }
  119. else if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == 0) {
  120. #if PS2_MOUSE_SCROLL_BTN_SEND
  121. if (scroll_state == SCROLL_BTN &&
  122. TIMER_DIFF_16(timer_read(), scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
  123. // send Scroll Button(down and up at once) when not scrolled
  124. mouse_report.buttons |= (PS2_MOUSE_SCROLL_BTN_MASK);
  125. host_mouse_send(&mouse_report);
  126. _delay_ms(100);
  127. mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
  128. }
  129. #endif
  130. scroll_state = SCROLL_NONE;
  131. }
  132. // doesn't send Scroll Button
  133. mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
  134. #endif
  135. host_mouse_send(&mouse_report);
  136. print_usb_data();
  137. }
  138. // clear report
  139. mouse_report.x = 0;
  140. mouse_report.y = 0;
  141. mouse_report.v = 0;
  142. mouse_report.h = 0;
  143. mouse_report.buttons = 0;
  144. }
  145. static void print_usb_data(void)
  146. {
  147. if (!debug_mouse) return;
  148. print("ps2_mouse usb: [");
  149. phex(mouse_report.buttons); print("|");
  150. print_hex8((uint8_t)mouse_report.x); print(" ");
  151. print_hex8((uint8_t)mouse_report.y); print(" ");
  152. print_hex8((uint8_t)mouse_report.v); print(" ");
  153. print_hex8((uint8_t)mouse_report.h); print("]\n");
  154. }
  155. /* PS/2 Mouse Synopsis
  156. * http://www.computer-engineering.org/ps2mouse/
  157. *
  158. * Command:
  159. * 0xFF: Reset
  160. * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
  161. * 0xF5: Disable Data Reporting
  162. * 0xF4: Enable Data Reporting
  163. * 0xF3: Set Sample Rate
  164. * 0xF2: Get Device ID
  165. * 0xF0: Set Remote Mode
  166. * 0xEB: Read Data
  167. * 0xEA: Set Stream Mode
  168. * 0xE9: Status Request
  169. * 0xE8: Set Resolution
  170. * 0xE7: Set Scaling 2:1
  171. * 0xE6: Set Scaling 1:1
  172. *
  173. * Mode:
  174. * Stream Mode: devices sends the data when it changs its state
  175. * Remote Mode: host polls the data periodically
  176. *
  177. * This code uses Remote Mode and polls the data with Read Data(0xEB).
  178. *
  179. * Data format:
  180. * byte|7 6 5 4 3 2 1 0
  181. * ----+--------------------------------------------------------------
  182. * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left
  183. * 1| X movement
  184. * 2| Y movement
  185. */