<spanclass="line"><span> CLK ------+------------ PIN</span></span></code></pre></div><h2id="busywait-version"tabindex="-1">Busywait Version <aclass="header-anchor"href="#busywait-version"aria-label="Permalink to "Busywait Version {#busywait-version}""></a></h2><p>Note: This is not recommended, you may encounter jerky movement or unsent inputs. Please use interrupt or USART version if possible.</p><p>In rules.mk:</p><divclass="language-make vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">make</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PS2_MOUSE_ENABLE = yes</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#endif</span></span></code></pre></div><h3id="interrupt-version-avr"tabindex="-1">Interrupt Version (AVR/ATMega32u4) <aclass="header-anchor"href="#interrupt-version-avr"aria-label="Permalink to "Interrupt Version (AVR/ATMega32u4) {#interrupt-version-avr}""></a></h3><p>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.</p><p>In rules.mk:</p><divclass="language-make vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">make</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PS2_MOUSE_ENABLE = yes</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#endif</span></span></code></pre></div><h3id="interrupt-version-chibios"tabindex="-1">Interrupt Version (ARM chibios) <aclass="header-anchor"href="#interrupt-version-chibios"aria-label="Permalink to "Interrupt Version (ARM chibios) {#interrupt-version-chibios}""></a></h3><p>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.</p><p>In rules.mk:</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>PS2_MOUSE_ENABLE = yes</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_DATA_PIN</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> A9</span></span></code></pre></div><p>And in the chibios specifig halconf.h:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PAL_USE_CALLBACKS</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> TRUE</span></span></code></pre></div><h3id="usart-version"tabindex="-1">USART Version <aclass="header-anchor"href="#usart-version"aria-label="Permalink to "USART Version {#usart-version}""></a></h3><p>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.</p><p>In rules.mk:</p><divclass="language-make vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">make</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PS2_MOUSE_ENABLE = yes</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#endif</span></span></code></pre></div><h3id="rp2040-pio-version"tabindex="-1">RP2040 PIO Version <aclass="header-anchor"href="#rp2040-pio-version"aria-label="Permalink to "RP2040 PIO Version {#rp2040-pio-version}""></a></h3><p>The <code>PIO</code> subsystem is a Raspberry Pi RP2040 specific implementation, using the integrated PIO peripheral and is therefore only available on this MCU.</p><p>There are strict requirements for pin ordering but any pair of GPIO pins can be used. The GPIO used for clock must be directly after data, see the included info.json snippet for an example of correct order.</p><p>You may optionally switch the PIO peripheral used with the following define in config.h:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_PIO_USE_PIO1</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Force the usage of PIO1 peripheral, by default the PS2 implementation uses the PIO0 peripheral</span></span></code></pre></div><p>Example info.json content:</p><divclass="language-json vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">json</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"ps2"</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span></code></pre></div><h2id="additional-settings"tabindex="-1">Additional Settings <aclass="header-anchor"href="#additional-settings"aria-label="Permalink to "Additional Settings {#additional-settings}""></a></h2><h3id="ps2-mouse-features"tabindex="-1">PS/2 Mouse Features <aclass="header-anchor"href="#ps2-mouse-features"aria-label="Permalink to "PS/2 Mouse Features {#ps2-mouse-features}""></a></h3><p>These enable settings supported by the PS/2 mouse protocol.</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Use remote mode instead of the default stream mode (see link) */</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Enable the scrollwheel or scroll gesture on your mouse or touchpad */</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Some mice will need a scroll mask to be configured. The default is 0xFF. */</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* Applies a transformation to the movement before sending to the host (see link) */</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* The time to wait after initializing the ps2 host */</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_INIT_DELAY</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1000</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* Default */</span></span></code></pre></div><p>You can also call the following functions from ps2_mouse.h</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> ps2_mouse_disable_data_reporting</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> ps2_mouse_set_sample_rate</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">ps2_mouse_sample_rate_t</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> sample_rate</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre></div><h3id="fine-control"tabindex="-1">Fine Control <aclass="header-anchor"href="#fine-control"aria-label="Permalink to "Fine Control {#fine-control}""></a></h3><p>Use the following defines to change the sensitivity and speed of the mouse. Note: you can also use <code>ps2_mouse_set_resolution</code> for the same effect (not supported on most touchpads).</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_X_MULTIPLIER</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_V_MULTIPLIER</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span></span></code></pre></div><h3id="scroll-button"tabindex="-1">Scroll Button <aclass="header-anchor"href="#scroll-button"aria-label="Permalink to "Scroll Button {#scroll-button}""></a></h3><p>If you're using a trackpoint, you will likely want to be able to use it for scrolling. It's possible to enable a "scroll button/s" that when pressed will cause the mouse to scroll instead of moving. To enable the feature, you must set a scroll button mask as follows:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_SCROLL_BTN_MASK</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"><<</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">PS2_MOUSE_BTN_MIDDLE)</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* Default */</span></span></code></pre></div><p>To disable the scroll button feature:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_SCROLL_BTN_MASK</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span></span></code></pre></div><p>The available buttons are:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_BTN_LEFT</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_BTN_MIDDLE</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span></span></code></pre></div><p>You can also combine buttons in the mask by <code>|</code>ing them together.</p><p>Once you've configured your scroll button mask, you must configure the scroll button send interval. This is the interval before which if the scroll buttons were released they would be sent to the host. After this interval, they will cause the mouse to scroll and will not be sent.</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_SCROLL_BTN_SEND</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 300</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* Default */</span></span></code></pre></div><p>To disable sending the scroll buttons:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_SCROLL_BTN_SEND</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span></span></code></pre></div><p>Fine control over the scrolling is supported with the following defines:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_SCROLL_DIVISOR_H</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_SCROLL_DIVISOR_V</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span></span></code></pre></div><h3id="invert-buttons"tabindex="-1">Invert Mouse buttons <aclass="header-anchor"href="#invert-buttons"aria-label="Permalink to "Invert Mouse buttons {#invert-buttons}""></a></h3><p>To invert the left & right buttons you can put:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_INVERT_BUTTONS</span></span></code></pre></div><p>into config.h.</p><h3id="invert-mouse-and-scroll-axes"tabindex="-1">Invert Mouse and Scroll Axes <aclass="header-anchor"href="#invert-mouse-and-scroll-axes"aria-label="Permalink to "Invert Mouse and Scroll Axes {#invert-mouse-and-scroll-axes}""></a></h3><p>To invert the X and Y axes you can put:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_INVERT_X</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_INVERT_Y</span></span></code></pre></div><p>into config.h.</p><p>To reverse the scroll axes you can put:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_INVERT_H</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_INVERT_V</span></span></code></pre></div><p>into config.h.</p><h3id="rotate-mouse-axes"tabindex="-1">Rotate Mouse Axes <aclass="header-anchor"href="#rotate-mouse-axes"aria-label="Permalink to "Rotate Mouse Axes {#rotate-mouse-axes}""></a></h3><p>Transform the output of the device with a clockwise rotation of 90, 180, or 270 degrees.</p><p>When compensating for device orientation, rotate the output the same amount in the opposite direction. E.g. if the normal device orientation is considered to be North-facing, compensate as follows:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_ROTATE</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 270</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* Compensate for East-facing device orientation. */</span></span></code></pre></div><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_ROTATE</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 180</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* Compensate for South-facing device orientation. */</span></span></code></pre></div><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_ROTATE</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 90</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* Compensate for West-facing device orientation. */</span></span></code></pre></div><h3id="debug-settings"tabindex="-1">Debug Settings <aclass="header-anchor"href="#debug-settings"aria-label="Permalink to "Debug Settings {#debug-settings}""></a></h3><p>To debug the mouse, add <code>debug_mouse = true</code> or enable via bootmagic.</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* To debug the mouse reports */</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#define</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PS2_MOUSE_DEBUG_RAW</span></span></code></pre></div><h3id="movement-hook"tabindex="-1">Movement Hook <aclass="header-anchor"href="#movement-hook"aria-label="Permalink to "Movement Hook {#movement-hook}""></a></h3><p>Process mouse movement in the keymap before it is sent to the host. Example uses include filtering noise, adding acceleration, and automatically activating a layer. To use, define the following function in your keymap:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">void</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> ps2_mouse_moved_user</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">report_mouse_t</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;">mouse_report</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span></code></pre></div></div></div></main><footerclass="VPDocFooter"data-v-39a288b8data-v-09de1c0f><!--[--><!--]--><!----><navclass="prev-next"data-v-09de1c0f><divclass="pager"data-v-09de1c0f><aclass="VPLink link pager-link prev"href="/features/pointing_device"data-v-09de1c0f><!--[--><spanclass="desc"data-v-09de1c0f>Previous page</span><spanclass="title"data-v-09de1c0f>Pointing Device</span><!--]--></a></div><divclass="pager"data-v-09de1c0f><aclass="VPLink link pager-link next"href="/features/split_keyboard"data-v-09de1c0f><!--[--><spanclass="desc"data-v-09de1c0f>Next page</span><spanclass="title"data-v-09de1c0f>Split Keyboard</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>