cancel
Showing results for 
Search instead for 
Did you mean: 

How to use 2 HIDs with 1 STM32?

HGaut.1
Associate II

Hello !

I am trying to make 1 STM32 that handles 2 HIDs, one keyboard (HID_MOUSE_ReportDescK[HID_MOUSE_REPORT_DESC_SIZEK]), one mouse (HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE]).

I use a STM32F103C8T6.

I tried to create 2 int (used as boolean) that permit to know if it has to be a keyboard or a mouse.

Those int are also used like that:

#if mouseEnabled == 1
 
 HID_MOUSE_REPORT_DESC_SIZE
 
 #elif keyboardEnabled == 1
 
 HID_MOUSE_REPORT_DESC_SIZEK,
 
#endif
 if (req->wValue >> 8 == HID_REPORT_DESC && mouseEnabled == 1)
          {
            len = MIN(HID_MOUSE_REPORT_DESC_SIZE, req->wLength);
            pbuf = HID_MOUSE_ReportDesc;
          }
          else if (req->wValue >> 8 == HID_REPORT_DESC && keyboardEnabled == 1)
                   {
                     len = MIN(HID_MOUSE_REPORT_DESC_SIZEK, req->wLength);
                     pbuf = HID_MOUSE_ReportDescK;
                   }

But, I am a beginner in the MCUs / programming world it's why I figured lately that what I made, works only when I change the int before debugging. I also tried many other things that came to fail.

Thank you for your help.

2 REPLIES 2
Pavel A.
Evangelist III

Modify the HID example so that it creates two separate USBH_ClassTypeDef objects,

assign each to the two host controllers:

USBH_ClassTypeDef hidClass1, hidClass2 ;

// Initialize these with HID class ...

.........

 USBH_RegisterClass(&hUsbHostHS, hidClass1);

 USBH_RegisterClass(&hUsbHostFS, hidClass2);

So each host controller has its own "class" object.

This way you can connect two HID devices to STM32.

-- pa

HGaut.1
Associate II

I maybe was unclear about that, but I don't want two HID devices. I would like to create a keyboard with a STM32 that can make also mouse tools (ex: traditional keyboard, though, up arrow increase the mouse cursor y axis.

About my main.c file, I initialize

extern USBD_HandleTypeDef hUsbDeviceFS;
uint8_t HID_buffer[8] = { 0 };

For the buttons part

if (HAL_GPIO_ReadPin( GPIOA, GPIO_PIN_15) == GPIO_PIN_SET)   // keyboard button
	 	  	        {
                                     // keyboardEnabled = 1;
	 	  	            HID_buffer[2] = 10;
	 	  	            USBD_HID_SendReport(&hUsbDeviceFS, HID_buffer, 8);
	 	  	        }
	 	  	        else{
 
                                     // keyboardEnabled = 0;
	 	  	            HID_buffer[2] = 0;
	 	  	            USBD_HID_SendReport(&hUsbDeviceFS, HID_buffer, 8);
	 	  	        }
	 	  	  if (HAL_GPIO_ReadPin( GPIOB, GPIO_PIN_5) == GPIO_PIN_SET) //mouse button
	 	  	       	{
                                     // mouseEnabled = 1;
	 	  	       	    HID_buffer[3] = 10;
	 	  	       	    USBD_HID_SendReport(&hUsbDeviceFS, HID_buffer, 8);
	 	  	       	}
	 	  	       	else{
                                    // mouseEnabled = 0;
	 	  	       	    HID_buffer[3] = 0;
	 	  	       	    USBD_HID_SendReport(&hUsbDeviceFS, HID_buffer, 8);
	 	  	       	}

I also use the HID class in STM32CubeIDE, not customHID one, maybe this is important to know.

Thank you some much for helping me.