0

I'm making a board with an atmega32u4 on it for the controller for a keyboard, and am having issues with telling the host computer that a key is pressed. I've modified the Keyboard demo supplied with the LUFA library, so that in theory I believe it should send the 'a' key constantly. Here's the relevant code from the source file:

int main(void)
{
    _delay_ms(100);

    SetupHardware();

    GlobalInterruptEnable();

    for (;;)
    {
        HID_Device_USBTask(&Keyboard_HID_Interface);
        USB_USBTask();
    }
}

bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
                                         uint8_t* const ReportID,
                                         const uint8_t ReportType,
                                         void* ReportData,
                                         uint16_t* const ReportSize)
{
    USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;

    uint8_t UsedKeyCodes = 0;

    KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_A;

    *ReportSize = sizeof(USB_KeyboardReport_Data_t);
    return true;
}

When I flash this code onto my board and plug it in to my computer, I get nothing (except, interestingly, the num lock led on my actual keyboard turns off and back on, no idea why). So, I decided to use wireshark to determine if anything at all is being sent between the computer and the microcontroller, and in fact a lot of messages are.

Here's a screenshot of part of it

USB address 3.72.1, in this case, is referring to my microcontroller board, which I confirmed with the lsusb command. I was surprised to see that the 'a' key is in fact being continuously sent to the host, and even more confused as to why the computer isn't registering that these keys are pressed. I've tried a few different things, such as returning true or false from the CreateHIDReport function, and using some logic to only send an 'a' key report every other call to CreateHIDReport, but none of these ideas helped.

Any idea how the computer can be picking up on these key reports, but not registering them as actual key presses?

Jacob Garby
  • 714
  • 9
  • 18

1 Answers1

0

Okay so I've got this working now, but honestly I'm not sure what I did differently. Just one of those things where if I fiddle around with the code enough it magically starts working. For the record, here's the working code:

int main(void)
{
    SetupHardware();

    // LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
    GlobalInterruptEnable();

    DDRF = 0x01; // gpio1 is output, rest are input
    PORTF = 0xfe; // gpio1 is low, rest are pull-up
    DDRD = 0xff;

    for (;;) {
            //_delay_ms(1000);
            HID_Device_USBTask(&Keyboard_HID_Interface);
            USB_USBTask();
    }
}

bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
                                         uint8_t* const ReportID,
                                         const uint8_t ReportType,
                                         void* ReportData,
                                         uint16_t* const ReportSize)

{

    USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;

    // uint8_t JoyStatus_LCL    = Joystick_GetStatus();
    // uint8_t ButtonStatus_LCL = Buttons_GetStatus();

    uint8_t UsedKeyCodes = 0;

    if (!(PINF & 0x2)) {
        //PORTD = 0xff;
    //if (JoyStatus_LCL & JOY_UP)
      KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_A;
    /*else if (JoyStatus_LCL & JOY_DOWN)
      KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_B;

    if (JoyStatus_LCL & JOY_LEFT)
      KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_C;
    else if (JoyStatus_LCL & JOY_RIGHT)
      KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_D;

    if (JoyStatus_LCL & JOY_PRESS)
      KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_E;

    if (ButtonStatus_LCL & BUTTONS_BUTTON1)
      KeyboardReport->KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_F;*/

    //if (UsedKeyCodes)
    //  KeyboardReiport->Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT;
    } else {
        //PORTD = 0x00;
    }

    *ReportSize = sizeof(USB_KeyboardReport_Data_t);
    return false;
}
```
Jacob Garby
  • 714
  • 9
  • 18
  • One thing different is you are now setting the port pin directions and pullups. What happens if you disable that code? – Bruce Abbott Jul 04 '21 at 22:29