dlkj/usbd-human-interface-device

Are there any ways to specify the polling rate?

Closed this issue · 3 comments

I'm working in a nkro keyboard, and while measuring polling rate it seems to be 62.5Hz, so I wanted to know if there is a way to overclock the polling rate at least up to 250 Hz (I couldn't find anything related in the crate)

dlkj commented

Hi @Asgragrt,

There are two things that affect the poll rate of the keyboard:

  1. The USB in endpoint poll interval
  2. How often your code/the microcontroller reads the key inputs

By default the in endpoint poll delay is 10ms. USB directions are relative to the host, so in is from the keyboard to the computer.

.in_endpoint(10.millis()))

You can reduce this by using a custom config like this:

    let config = NKROBootKeyboardConfig::new(ManagedIdleInterfaceConfig::new(
        unwrap!(unwrap!(unwrap!(unwrap!(InterfaceBuilder::new(
            NKRO_BOOT_KEYBOARD_REPORT_DESCRIPTOR
        ))
        .description("NKRO Keyboard")
        .boot_device(InterfaceProtocol::Keyboard)
        .idle_default(500.millis()))
        .in_endpoint(1.millis()))
        .with_out_endpoint(100.millis()))
        .build(),
    ));

    let mut keyboard = UsbHidClassBuilder::new().add_device(config).build(&usb_bus);

For the 2nd one, you'll want to use interrupts to get this to an absolute minimum, but you can probably get quite close by removing any poll waits/delays in the main loop.

For keyboard_nkro.rs:
Reduce this line to 10.millis() or even something smaller like 100.nanos().

input_count_down.start(10.millis());

For keyboard_rtic.rs:
Reduce this line similarly

let next = scheduled + 50.millis();

If you really want to minimise latency, look at the rtic or irq based examples. You'll also want to look at how to enable and use gpio interrupts as this isn't something shown in the examples.

Hi.
As you specified, with the custom config and the reduction of the input_count_down the polling rate seems to be higher.
Thank you so much for your fast and appropriate response on GitHub! I genuinely appreciate your prompt attention and the valuable assistance you provided!!

dlkj commented

Great stuff. Thanks for the nice feedback.