cancel
Showing results for 
Search instead for 
Did you mean: 

PROBLEM TO RECEIVE FROM HOST TO USB HID KEYBOARD

shalaka
Associate

I want to receive data from my host PC through USB. I'm using the STM32C0 series nucleo board as USB HID device. I have assigned the value of capslock to the user button. and when the button is pressed i want to turn on the user led on the board. the problem is that when i press the user key on stm32 it glows the led but when i press the capslock key on another hid keyboard attaced to my pc then the output is not reflected on the stm32 board. that means it is not receiveing data from host properly . if it's not receiving data properly then why it is turned on by user button . my endpoint settings are as follows. 

 

/* USER CODE BEGIN USB_Device_Init_PreTreatment_1 */

HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x00, PCD_SNG_BUF, 0x0C);

HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x80, PCD_SNG_BUF, 0x4C);

HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x81, PCD_SNG_BUF, 0x8C); // EP1 IN

HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x02, PCD_SNG_BUF, 0xCC); // EP1 OUT

/* USER CODE END USB_Device_Init_PreTreatment_1 */

 

and the code for the keypress logic transmit and receive is attached below.

1 ACCEPTED SOLUTION

Accepted Solutions
shalaka
Associate

The LED output reports are sent by the host on control out endpoint EP0 which 0x00. whenever data is received on the control endpoint a function called "setreport" in the keyboard.c file is called by the STM's USB stack. I implemented the logic for comparing the led report value for numlock, capslock and scrolllock keys in that function and it worked. now my device can send and receive the usb data properly. the function that modified for my application is as follows.

/**

* @brief USBD_HID_Keyboard_SetReport

* This function is invoked when the host sends a HID SET_REPORT

* to the application over Endpoint 0.

* @param hid_instance: Pointer to the hid class instance.

* @param hid_event: Pointer to structure of the hid event.

* @retval status

*/

UINT USBD_HID_Keyboard_SetReport(UX_SLAVE_CLASS_HID *hid_instance,

UX_SLAVE_CLASS_HID_EVENT *hid_event)

{

UINT status = UX_SUCCESS;

 

/* USER CODE BEGIN USBD_HID_Keyboard_SetReport */

/* Extract the report data from the hid_event */

uint8_t report_id = hid_event->ux_device_class_hid_event_report_id; // Report ID (if used)

uint8_t led_state = hid_event->ux_device_class_hid_event_buffer[0]; // LED state data

 

/* Process LED states */

if (led_state & 0x01) {

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Turn on Num Lock LED

} else {

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Turn off Num Lock LED

}

 

if (led_state & 0x02) {

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET); // Turn on Caps Lock LED

} else {

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET); // Turn off Caps Lock LED

}

 

if (led_state & 0x04) {

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET); // Turn on Scroll Lock LED

} else {

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // Turn off Scroll Lock LED

}

/* USER CODE END USBD_HID_Keyboard_SetReport */

 

return status;

}

View solution in original post

1 REPLY 1
shalaka
Associate

The LED output reports are sent by the host on control out endpoint EP0 which 0x00. whenever data is received on the control endpoint a function called "setreport" in the keyboard.c file is called by the STM's USB stack. I implemented the logic for comparing the led report value for numlock, capslock and scrolllock keys in that function and it worked. now my device can send and receive the usb data properly. the function that modified for my application is as follows.

/**

* @brief USBD_HID_Keyboard_SetReport

* This function is invoked when the host sends a HID SET_REPORT

* to the application over Endpoint 0.

* @param hid_instance: Pointer to the hid class instance.

* @param hid_event: Pointer to structure of the hid event.

* @retval status

*/

UINT USBD_HID_Keyboard_SetReport(UX_SLAVE_CLASS_HID *hid_instance,

UX_SLAVE_CLASS_HID_EVENT *hid_event)

{

UINT status = UX_SUCCESS;

 

/* USER CODE BEGIN USBD_HID_Keyboard_SetReport */

/* Extract the report data from the hid_event */

uint8_t report_id = hid_event->ux_device_class_hid_event_report_id; // Report ID (if used)

uint8_t led_state = hid_event->ux_device_class_hid_event_buffer[0]; // LED state data

 

/* Process LED states */

if (led_state & 0x01) {

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Turn on Num Lock LED

} else {

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Turn off Num Lock LED

}

 

if (led_state & 0x02) {

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET); // Turn on Caps Lock LED

} else {

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET); // Turn off Caps Lock LED

}

 

if (led_state & 0x04) {

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET); // Turn on Scroll Lock LED

} else {

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // Turn off Scroll Lock LED

}

/* USER CODE END USBD_HID_Keyboard_SetReport */

 

return status;

}