cancel
Showing results for 
Search instead for 
Did you mean: 

Minimum HID mouse test code?

MM..1
Chief III

I try on nucleo F446

  MX_USB_HOST_Init();
  /* USER CODE BEGIN 2 */
  USBH_ReEnumerate(&hUsbHostFS);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
 
    if(USBH_HID_GetDeviceType(&hUsbHostFS) == HID_MOUSE)   m_pinfo = USBH_HID_GetMouseInfo(&hUsbHostFS);
 
  }
  /* USER CODE END 3 */

but m_pinfo dont show move or buttons. What i miss.

1 ACCEPTED SOLUTION

Accepted Solutions

Ofcourse. I solve moving get into HID callback. Now all works ok.

Why MX dont place callback into generated main?

/* USER CODE BEGIN 0 */
char Uart_Buf[200];
void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
	if(USBH_HID_GetDeviceType(phost) == HID_MOUSE)  // if the HID is Mouse
	{
		HID_MOUSE_Info_TypeDef *Mouse_Info;
		Mouse_Info = USBH_HID_GetMouseInfo(phost);  // Get the info
		int len = sprintf (Uart_Buf, "X=%d, Y=%d, Button1=%d, Button2=%d, Button3=%d\r\n", (int8_t)Mouse_Info->x, (int8_t)Mouse_Info->y, Mouse_Info->buttons[0],Mouse_Info->buttons[1], Mouse_Info->buttons[2]);
		HAL_UART_Transmit(&huart3, (uint8_t *) Uart_Buf, len, 100);
	}
}
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_USB_HOST_Init();
  /* USER CODE BEGIN 2 */
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

View solution in original post

3 REPLIES 3

Is the enumeration succeeding?

Is the mouse getting power?

Ofcourse. I solve moving get into HID callback. Now all works ok.

Why MX dont place callback into generated main?

/* USER CODE BEGIN 0 */
char Uart_Buf[200];
void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
	if(USBH_HID_GetDeviceType(phost) == HID_MOUSE)  // if the HID is Mouse
	{
		HID_MOUSE_Info_TypeDef *Mouse_Info;
		Mouse_Info = USBH_HID_GetMouseInfo(phost);  // Get the info
		int len = sprintf (Uart_Buf, "X=%d, Y=%d, Button1=%d, Button2=%d, Button3=%d\r\n", (int8_t)Mouse_Info->x, (int8_t)Mouse_Info->y, Mouse_Info->buttons[0],Mouse_Info->buttons[1], Mouse_Info->buttons[2]);
		HAL_UART_Transmit(&huart3, (uint8_t *) Uart_Buf, len, 100);
	}
}
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_USB_HOST_Init();
  /* USER CODE BEGIN 2 */
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

@MM..1​ "Why MX dont place callback into generated main?"

Because it has no way to know where you want it to happen in your code?

"Now all works ok"

You know what to do:

0693W000008y9fZQAQ.png