Skip to main content
MM..1
Super User
July 28, 2022
Solved

Minimum HID mouse test code?

  • July 28, 2022
  • 3 replies
  • 879 views

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.

    This topic has been closed for replies.
    Best answer by MM..1

    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 */
    }

    3 replies

    Andrew Neil
    Super User
    July 28, 2022

    Is the enumeration succeeding?

    Is the mouse getting power?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    MM..1
    MM..1AuthorBest answer
    Super User
    July 28, 2022

    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 */
    }

    Andrew Neil
    Super User
    July 28, 2022

    @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

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.