8000 Allow changing USB HID poll rate (#1771) · abdukir/arduino-pico@ae6847c · GitHub
[go: up one dir, main page]

Skip to content

Commit ae6847c

Browse files
Allow changing USB HID poll rate (earlephilhower#1771)
Fixes earlephilhower#1769 Add a weak variable that can be overridden by the user to speed up or slow down the USB HID polling speed.
1 parent d42f0ab commit ae6847c

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

cores/rp2040/RP2040USB.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ static int __usb_task_irq;
8383
9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, TUSB_CLASS_VENDOR_SPECIFIC, RESET_INTERFACE_SUBCLASS, RESET_INTERFACE_PROTOCOL, _stridx,
8484

8585

86+
int usb_hid_poll_interval __attribute__((weak)) = 10;
87+
8688
const uint8_t *tud_descriptor_device_cb(void) {
8789
static tusb_desc_device_t usbd_desc_device = {
8890
.bLength = sizeof(tusb_desc_device_t),
@@ -263,7 +265,7 @@ void __SetupUSBDescriptor() {
263265
uint8_t hid_itf = __USBInstallSerial ? 2 : 0;
264266
uint8_t hid_desc[TUD_HID_DESC_LEN] = {
265267
// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
266-
TUD_HID_DESCRIPTOR(hid_itf, 0, HID_ITF_PROTOCOL_NONE, hid_report_len, EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
268+
TUD_HID_DESCRIPTOR(hid_itf, 0, HID_ITF_PROTOCOL_NONE, hid_report_len, EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, (uint8_t)usb_hid_poll_interval)
267269
};
268270

269271
uint8_t msd_itf = interface_count - 1;
@@ -400,7 +402,7 @@ bool __USBHIDReady() {
400402

401403
while (((millis() - start) < timeout) && tud_ready() && !tud_hid_ready()) {
402404
tud_task();
403-
delay(1);
405+
delayMicroseconds(1);
404406
}
405407
return tud_hid_ready();
406408
}

docs/usb.rst

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,28 @@ from the IDE.
1414
The Arduino-Pico core includes ported versions of the basic Arduino
1515
``Keyboard``, ``Mouse`` and ``Joystick`` libraries. These libraries
1616
allow you to emulate a keyboard, a gamepad or mouse (or all together)
17-
with the Pico in your sketches.
17+
with the Pico in your sketches. These libraries only are available
18+
when using the built-in USB, not the Adafruit library.
1819

1920
See the examples and Arduino Reference at
2021
https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
2122
and
2223
https://www.arduino.cc/reference/en/language/functions/usb/mouse
2324

25+
HID Polling Interval
26+
--------------------
27+
By default, HID devices will request to be polled every 10ms (i.e. 100x
28+
per second). If you have a higher performance need, you can override
29+
this value by creating a global variable in your main application, set
30+
to the polling period:
31+
32+
.. code:: cpp
33+
34+
int usb_hid_poll_interval = 1; // Set HID poll interval to 1ms (1kHz)
35+
void setup() {
36+
....
37+
}
38+
2439
Adafruit TinyUSB Arduino Support
2540
--------------------------------
2641
Examples are provided in the Adafruit_TinyUSB_Arduino for the more

0 commit comments

Comments
 (0)
0