Patchouli9
Patchouli9

Reputation: 101

HID Keyboard Not Sending Keystrokes Using Raspberry Pi Pico - Device Recognized but No Input

I am trying to implement a custom HID device using a Raspberry Pi Pico that simulates a keyboard. I have configured the GPIO1 pin to act as a button, where pressing the button should send a keypress (Shift + 'A') and releasing the button should send a key release report.

Here’s what I’ve done so far:

Firmware Code:

#include "hardware/irq.h"
#include "pico/stdlib.h"
#include "tusb.h"

#define KEY_SHIFT 0x02
#define KEY_A 0x04

#define USB_VID 0xCAFE
#define USB_PID 0x000A
tusb_desc_device_t const desc_device = {
    .bLength = sizeof(tusb_desc_device_t),
    .bDescriptorType = TUSB_DESC_DEVICE,
    .bcdUSB = 0x0200,
    .bDeviceClass = 0xEF,
    .bDeviceSubClass = 0x02,
    .bDeviceProtocol = 0x01,
    .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,

    .idVendor = USB_VID,
    .idProduct = USB_PID,
    .bcdDevice = 0x0100,

    .iManufacturer = 0x01,
    .iProduct = 0x02,
    .iSerialNumber = 0x03,

    .bNumConfigurations = 0x01};

// HID report descriptor taken from a HORI Fighting Stick V3
// with feature and output bits omitted
// works out of the box with PC and PS3
// as dumped by usbhid-dump and parsed by
// https://eleccelerator.com/usbdescreqparser/
const uint8_t desc_hid_report[] = {
    0x05, 0x01,  // Usage Page (Generic Desktop)
    0x09, 0x06,  // Usage (Keyboard)
    0xA1, 0x01,  // Collection (Application)

    // Modifier Keys (8 bits)
    0x05, 0x07,  //   Usage Page (Keyboard)
    0x19, 0xE0,  //   Usage Minimum (Left Control)
    0x29, 0xE7,  //   Usage Maximum (Right GUI)
    0x15, 0x00,  //   Logical Minimum (0)
    0x25, 0x01,  //   Logical Maximum (1)
    0x75, 0x01,  //   Report Size (1)
    0x95, 0x08,  //   Report Count (8)
    0x81, 0x02,  //   Input (Data, Variable, Absolute) - Modifier keys

    // Reserved Byte
    0x75, 0x08,  //   Report Size (8)
    0x95, 0x01,  //   Report Count (1)
    0x81, 0x03,  //   Input (Constant, Variable, Absolute) - Reserved

    // Keycode Array (6 bytes)
    0x75, 0x08,  //   Report Size (8)
    0x95, 0x06,  //   Report Count (6)
    0x15, 0x00,  //   Logical Minimum (0)
    0x25, 0x65,  //   Logical Maximum (101) - Max keycode from HID Usage Table
    0x05, 0x07,  //   Usage Page (Keyboard)
    0x19, 0x00,  //   Usage Minimum (0)
    0x29, 0x65,  //   Usage Maximum (101)
    0x81, 0x00,  //   Input (Data, Array) - Key array

    // LED Output Report (Caps Lock, Num Lock, etc.)
    0x75, 0x01,  //   Report Size (1)
    0x95, 0x05,  //   Report Count (5)
    0x05, 0x08,  //   Usage Page (LEDs)
    0x19, 0x01,  //   Usage Minimum (Num Lock)
    0x29, 0x05,  //   Usage Maximum (Kana)
    0x25, 0x01,  //   Logical Maximum (1) <- Added
    0x91, 0x02,  //   Output (Data, Variable, Absolute) - LED states

    // LED Padding Bits
    0x75, 0x03,  //   Report Size (3)
    0x95, 0x01,  //   Report Count (1)
    0x91, 0x03,  //   Output (Constant, Variable, Absolute) - LED padding

    0xC0  // End Collection
};

#define CONFIG_TOTAL_LEN \
    (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN + TUD_CDC_DESC_LEN)
#define EPNUM_HID 0x83
#define EPNUM_CDC_NOTIF 0x81
#define EPNUM_CDC_OUT 0x02
#define EPNUM_CDC_IN 0x82

uint8_t const desc_configuration[] = {
    // Config number, interface count, string index, total length,
    // attribute,
    // power in mA
    TUD_CONFIG_DESCRIPTOR(1, 3, 0, CONFIG_TOTAL_LEN, 0, 100),

    // Interface number, string index, protocol, report descriptor len, EP
    // In
    // address, size & polling interval
    TUD_HID_DESCRIPTOR(0,
                       4,
                       HID_ITF_PROTOCOL_NONE,
                       sizeof(desc_hid_report),
                       EPNUM_HID,
                       CFG_TUD_HID_EP_BUFSIZE,
                       1),

    TUD_CDC_DESCRIPTOR(1,
                       5,
                       EPNUM_CDC_NOTIF,
                       8,
                       EPNUM_CDC_OUT,
                       EPNUM_CDC_IN,
                       64)};

// Invoked when received GET DEVICE DESCRIPTOR
// Application return pointer to descriptor
uint8_t const* tud_descriptor_device_cb(void) {
    return (uint8_t const*)&desc_device;
}

// Invoked when received GET HID REPORT DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const* tud_hid_descriptor_report_cb(uint8_t itf) {
    return desc_hid_report;
}

// Invoked when received GET_REPORT control request
// Application must fill buffer report's content and return its length.
// Return zero will cause the stack to STALL request
uint16_t tud_hid_get_report_cb(uint8_t itf,
                               uint8_t report_id,
                               hid_report_type_t report_type,
                               uint8_t* buffer,
                               uint16_t reqlen) {
    return 0;
}

// Invoked when received SET_REPORT control request or
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
void tud_hid_set_report_cb(uint8_t itf,
                           uint8_t report_id,
                           hid_report_type_t report_type,
                           uint8_t const* buffer,
                           uint16_t bufsize) {}

// Invoked when received GET CONFIGURATION DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const* tud_descriptor_configuration_cb(uint8_t index) {
    return desc_configuration;
}

char const* string_desc_arr[] = {
    (const char[]){0x09, 0x04},  // 0: is supported language is English (0x0409)
    "AwA",                       // 1: Manufacturer
    "ComposeBoard",              // 2: Product
    "123456",                    // 3: Serials, should use chip ID
};

static uint16_t _desc_str[32];

#define BUTTON_PIN 1
bool pressed = 0;
#define DEBOUNCE_DELAY_MS 50
static uint64_t last_debounce_time = 0;

void hid_task() {
    if (!tud_hid_ready()) {
        return;
    }

    bool button_state = gpio_get(BUTTON_PIN) == 0;

    uint64_t current_time_us = time_us_64();

    uint64_t current_time = current_time_us / 1000;

    if ((current_time - last_debounce_time) < DEBOUNCE_DELAY_MS)
        return;

    last_debounce_time = current_time;

    if (pressed != button_state) {
        pressed = button_state;

        if (button_state) {
            uint8_t keycode[6] = {KEY_A, 0, 0, 0, 0, 0};
            tud_hid_keyboard_report(0, KEY_SHIFT, keycode);
            printf("Pressed\n");
        } else {
            uint8_t empty_keycode[6] = {0};
            tud_hid_keyboard_report(0, 0, empty_keycode);
            printf("Released\n");
        }
    }
}

int main(void) {
    stdio_init_all();

    gpio_init(BUTTON_PIN);
    gpio_set_dir(BUTTON_PIN, GPIO_IN);
    gpio_pull_up(BUTTON_PIN);

    tusb_init();

    while (1) {
        tud_task();
        hid_task();
        // usb_cdc_task();
    }

    return 0;
}

// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long
// enough for transfer to complete
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
    uint8_t chr_count;

    if (index == 0) {
        memcpy(&_desc_str[1], string_desc_arr[0], 2);
        chr_count = 1;
    } else {
        // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
        // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors

        if (!(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])))
            return NULL;

        const char* str = string_desc_arr[index];

        // Cap at max char
        chr_count = strlen(str);
        if (chr_count > 31)
            chr_count = 31;

        // Convert ASCII string into UTF-16
        for (uint8_t i = 0; i < chr_count; i++) {
            _desc_str[1 + i] = str[i];
        }
    }

    // first byte is length (including header), second byte is string type
    _desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * chr_count + 2);

    return _desc_str;
}

Problem: The device is recognized, but pressing the button does not register any input in the operating system.

Can anyone help me identify what might be going wrong? What am I missing in terms of HID implementation on the Raspberry Pi Pico?

Upvotes: 0

Views: 36

Answers (0)

Related Questions