You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
8.9 KiB

  1. # PS/2 Mouse Support :id=ps2-mouse-support
  2. Its possible to hook up a PS/2 mouse (for example touchpads or trackpoints) to your keyboard as a composite device.
  3. To hook up a Trackpoint, you need to obtain a Trackpoint module (i.e. harvest from a Thinkpad keyboard), identify the function of each pin of the module, and make the necessary circuitry between controller and Trackpoint module. For more information, please refer to [Trackpoint Hardware](https://deskthority.net/wiki/TrackPoint_Hardware) page on Deskthority Wiki.
  4. There are three available modes for hooking up PS/2 devices: USART (best), interrupts (better) or busywait (not recommended).
  5. ## The Circuitry between Trackpoint and Controller :id=the-circuitry-between-trackpoint-and-controller
  6. To get the things working, a 4.7K drag is needed between the two lines DATA and CLK and the line 5+.
  7. ```
  8. DATA ----------+--------- PIN
  9. |
  10. 4.7K
  11. |
  12. MODULE 5+ --------+--+--------- PWR CONTROLLER
  13. |
  14. 4.7K
  15. |
  16. CLK ------+------------ PIN
  17. ```
  18. ## Busywait Version :id=busywait-version
  19. Note: This is not recommended, you may encounter jerky movement or unsent inputs. Please use interrupt or USART version if possible.
  20. In rules.mk:
  21. ```makefile
  22. PS2_MOUSE_ENABLE = yes
  23. PS2_USE_BUSYWAIT = yes
  24. ```
  25. In your keyboard config.h:
  26. ```c
  27. #ifdef PS2_USE_BUSYWAIT
  28. # define PS2_CLOCK_PORT PORTD
  29. # define PS2_CLOCK_PIN PIND
  30. # define PS2_CLOCK_DDR DDRD
  31. # define PS2_CLOCK_BIT 1
  32. # define PS2_DATA_PORT PORTD
  33. # define PS2_DATA_PIN PIND
  34. # define PS2_DATA_DDR DDRD
  35. # define PS2_DATA_BIT 2
  36. #endif
  37. ```
  38. ### Interrupt Version (AVR/ATMega32u4) :id=interrupt-version-avr
  39. The following example uses D2 for clock and D5 for data. You can use any INT or PCINT pin for clock, and any pin for data.
  40. In rules.mk:
  41. ```makefile
  42. PS2_MOUSE_ENABLE = yes
  43. PS2_USE_INT = yes
  44. ```
  45. In your keyboard config.h:
  46. ```c
  47. #ifdef PS2_USE_INT
  48. #define PS2_CLOCK_PORT PORTD
  49. #define PS2_CLOCK_PIN PIND
  50. #define PS2_CLOCK_DDR DDRD
  51. #define PS2_CLOCK_BIT 2
  52. #define PS2_DATA_PORT PORTD
  53. #define PS2_DATA_PIN PIND
  54. #define PS2_DATA_DDR DDRD
  55. #define PS2_DATA_BIT 5
  56. #define PS2_INT_INIT() do { \
  57. EICRA |= ((1<<ISC21) | \
  58. (0<<ISC20)); \
  59. } while (0)
  60. #define PS2_INT_ON() do { \
  61. EIMSK |= (1<<INT2); \
  62. } while (0)
  63. #define PS2_INT_OFF() do { \
  64. EIMSK &= ~(1<<INT2); \
  65. } while (0)
  66. #define PS2_INT_VECT INT2_vect
  67. #endif
  68. ```
  69. ### Interrupt Version (ARM chibios) :id=interrupt-version-chibios
  70. Pretty much any two pins can be used for the (software) interrupt variant on ARM cores. The example below uses A8 for clock, and A9 for data.
  71. In rules.mk:
  72. ```
  73. PS2_MOUSE_ENABLE = yes
  74. PS2_USE_INT = yes
  75. ```
  76. In your keyboard config.h:
  77. ```c
  78. #define PS2_CLOCK A8
  79. #define PS2_DATA A9
  80. ```
  81. And in the chibios specifig halconf.h:
  82. ```c
  83. #define PAL_USE_CALLBACKS TRUE
  84. ```
  85. ### USART Version :id=usart-version
  86. To use USART on the ATMega32u4, you have to use PD5 for clock and PD2 for data. If one of those are unavailable, you need to use interrupt version.
  87. In rules.mk:
  88. ```makefile
  89. PS2_MOUSE_ENABLE = yes
  90. PS2_USE_USART = yes
  91. ```
  92. In your keyboard config.h:
  93. ```c
  94. #ifdef PS2_USE_USART
  95. #define PS2_CLOCK_PORT PORTD
  96. #define PS2_CLOCK_PIN PIND
  97. #define PS2_CLOCK_DDR DDRD
  98. #define PS2_CLOCK_BIT 5
  99. #define PS2_DATA_PORT PORTD
  100. #define PS2_DATA_PIN PIND
  101. #define PS2_DATA_DDR DDRD
  102. #define PS2_DATA_BIT 2
  103. /* synchronous, odd parity, 1-bit stop, 8-bit data, sample at falling edge */
  104. /* set DDR of CLOCK as input to be slave */
  105. #define PS2_USART_INIT() do { \
  106. PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT); \
  107. PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT); \
  108. UCSR1C = ((1 << UMSEL10) | \
  109. (3 << UPM10) | \
  110. (0 << USBS1) | \
  111. (3 << UCSZ10) | \
  112. (0 << UCPOL1)); \
  113. UCSR1A = 0; \
  114. UBRR1H = 0; \
  115. UBRR1L = 0; \
  116. } while (0)
  117. #define PS2_USART_RX_INT_ON() do { \
  118. UCSR1B = ((1 << RXCIE1) | \
  119. (1 << RXEN1)); \
  120. } while (0)
  121. #define PS2_USART_RX_POLL_ON() do { \
  122. UCSR1B = (1 << RXEN1); \
  123. } while (0)
  124. #define PS2_USART_OFF() do { \
  125. UCSR1C = 0; \
  126. UCSR1B &= ~((1 << RXEN1) | \
  127. (1 << TXEN1)); \
  128. } while (0)
  129. #define PS2_USART_RX_READY (UCSR1A & (1<<RXC1))
  130. #define PS2_USART_RX_DATA UDR1
  131. #define PS2_USART_ERROR (UCSR1A & ((1<<FE1) | (1<<DOR1) | (1<<UPE1)))
  132. #define PS2_USART_RX_VECT USART1_RX_vect
  133. #endif
  134. ```
  135. ## Additional Settings :id=additional-settings
  136. ### PS/2 Mouse Features :id=ps2-mouse-features
  137. These enable settings supported by the PS/2 mouse protocol.
  138. ```c
  139. /* Use remote mode instead of the default stream mode (see link) */
  140. #define PS2_MOUSE_USE_REMOTE_MODE
  141. /* Enable the scrollwheel or scroll gesture on your mouse or touchpad */
  142. #define PS2_MOUSE_ENABLE_SCROLLING
  143. /* Some mice will need a scroll mask to be configured. The default is 0xFF. */
  144. #define PS2_MOUSE_SCROLL_MASK 0x0F
  145. /* Applies a transformation to the movement before sending to the host (see link) */
  146. #define PS2_MOUSE_USE_2_1_SCALING
  147. /* The time to wait after initializing the ps2 host */
  148. #define PS2_MOUSE_INIT_DELAY 1000 /* Default */
  149. ```
  150. You can also call the following functions from ps2_mouse.h
  151. ```c
  152. void ps2_mouse_disable_data_reporting(void);
  153. void ps2_mouse_enable_data_reporting(void);
  154. void ps2_mouse_set_remote_mode(void);
  155. void ps2_mouse_set_stream_mode(void);
  156. void ps2_mouse_set_scaling_2_1(void);
  157. void ps2_mouse_set_scaling_1_1(void);
  158. void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution);
  159. void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate);
  160. ```
  161. ### Fine Control :id=fine-control
  162. Use the following defines to change the sensitivity and speed of the mouse.
  163. Note: you can also use `ps2_mouse_set_resolution` for the same effect (not supported on most touchpads).
  164. ```c
  165. #define PS2_MOUSE_X_MULTIPLIER 3
  166. #define PS2_MOUSE_Y_MULTIPLIER 3
  167. #define PS2_MOUSE_V_MULTIPLIER 1
  168. ```
  169. ### Scroll Button :id=scroll-button
  170. If you're using a trackpoint, you will likely want to be able to use it for scrolling.
  171. It's possible to enable a "scroll button/s" that when pressed will cause the mouse to scroll instead of moving.
  172. To enable the feature, you must set a scroll button mask as follows:
  173. ```c
  174. #define PS2_MOUSE_SCROLL_BTN_MASK (1<<PS2_MOUSE_BTN_MIDDLE) /* Default */
  175. ```
  176. To disable the scroll button feature:
  177. ```c
  178. #define PS2_MOUSE_SCROLL_BTN_MASK 0
  179. ```
  180. The available buttons are:
  181. ```c
  182. #define PS2_MOUSE_BTN_LEFT 0
  183. #define PS2_MOUSE_BTN_RIGHT 1
  184. #define PS2_MOUSE_BTN_MIDDLE 2
  185. ```
  186. You can also combine buttons in the mask by `|`ing them together.
  187. Once you've configured your scroll button mask, you must configure the scroll button send interval.
  188. This is the interval before which if the scroll buttons were released they would be sent to the host.
  189. After this interval, they will cause the mouse to scroll and will not be sent.
  190. ```c
  191. #define PS2_MOUSE_SCROLL_BTN_SEND 300 /* Default */
  192. ```
  193. To disable sending the scroll buttons:
  194. ```c
  195. #define PS2_MOUSE_SCROLL_BTN_SEND 0
  196. ```
  197. Fine control over the scrolling is supported with the following defines:
  198. ```c
  199. #define PS2_MOUSE_SCROLL_DIVISOR_H 2
  200. #define PS2_MOUSE_SCROLL_DIVISOR_V 2
  201. ```
  202. ### Invert Mouse buttons :id=invert-buttons
  203. To invert the left & right buttons you can put:
  204. ```c
  205. #define PS2_MOUSE_INVERT_BUTTONS
  206. ```
  207. into config.h.
  208. ### Invert Mouse and Scroll Axes :id=invert-mouse-and-scroll-axes
  209. To invert the X and Y axes you can put:
  210. ```c
  211. #define PS2_MOUSE_INVERT_X
  212. #define PS2_MOUSE_INVERT_Y
  213. ```
  214. into config.h.
  215. To reverse the scroll axes you can put:
  216. ```c
  217. #define PS2_MOUSE_INVERT_H
  218. #define PS2_MOUSE_INVERT_V
  219. ```
  220. into config.h.
  221. ### Rotate Mouse Axes :id=rotate-mouse-axes
  222. Transform the output of the device with a clockwise rotation of 90, 180, or 270
  223. degrees.
  224. When compensating for device orientation, rotate the output the same amount in
  225. the opposite direction. E.g. if the normal device orientation is considered to
  226. be North-facing, compensate as follows:
  227. ```c
  228. #define PS2_MOUSE_ROTATE 270 /* Compensate for East-facing device orientation. */
  229. ```
  230. ```c
  231. #define PS2_MOUSE_ROTATE 180 /* Compensate for South-facing device orientation. */
  232. ```
  233. ```c
  234. #define PS2_MOUSE_ROTATE 90 /* Compensate for West-facing device orientation. */
  235. ```
  236. ### Debug Settings :id=debug-settings
  237. To debug the mouse, add `debug_mouse = true` or enable via bootmagic.
  238. ```c
  239. /* To debug the mouse reports */
  240. #define PS2_MOUSE_DEBUG_HID
  241. #define PS2_MOUSE_DEBUG_RAW
  242. ```
  243. ### Movement Hook :id=movement-hook
  244. Process mouse movement in the keymap before it is sent to the host. Example
  245. uses include filtering noise, adding acceleration, and automatically activating
  246. a layer. To use, define the following function in your keymap:
  247. ```c
  248. void ps2_mouse_moved_user(report_mouse_t *mouse_report);
  249. ```