Stm32f7-Disco as a usb mouse

Description: Stm32f7-Disco as a usb mouse. This post shows how to use Stm32f7 - Disco board as a usb mouse.

This post shows how to use Stm32f7 - Disco board as a usb mouse.

Please view this post to configure HID  usb device .Do  the same to generate code. After generate code :


 In /../Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src
edit file usbd_hid.c


................................
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ]  __ALIGN_END =
{
  0x09, /* bLength: Configuration Descriptor size */
  USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
  USB_HID_CONFIG_DESC_SIZ,
  /* wTotalLength: Bytes returned */
  0x00,
  0x01,         /*bNumInterfaces: 1 interface*/
  0x01,         /*bConfigurationValue: Configuration value*/
  0x00,         /*iConfiguration: Index of string descriptor describing
  the configuration*/
  0xE0,         /*bmAttributes: bus powered and Support Remote Wake-up */
  0x32,         /*MaxPower 100 mA: this current is used for detecting Vbus*/
  
  /************** Descriptor of Joystick Mouse interface ****************/
  /* 09 */
  0x09,         /*bLength: Interface Descriptor size*/
  USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
  0x00,         /*bInterfaceNumber: Number of Interface*/
  0x00,         /*bAlternateSetting: Alternate setting*/
  0x01,         /*bNumEndpoints*/
  0x03,         /*bInterfaceClass: HID*/
  0x01,         /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
  0x02,         /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
  0,            /*iInterface: Index of string descriptor*/
  /******************** Descriptor of Joystick Mouse HID ********************/
  /* 18 */
  0x09,         /*bLength: HID Descriptor size*/
  HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
  0x11,         /*bcdHID: HID Class Spec release number*/
  0x01,
  0x00,         /*bCountryCode: Hardware target country*/
  0x01,         /*bNumDescriptors: Number of HID class descriptors to follow*/
  0x22,         /*bDescriptorType*/
  HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
  0x00,
  /******************** Descriptor of Mouse endpoint ********************/
  /* 27 */
  0x07,          /*bLength: Endpoint Descriptor size*/
  USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
  
  HID_EPIN_ADDR,     /*bEndpointAddress: Endpoint Address (IN)*/
  0x03,          /*bmAttributes: Interrupt endpoint*/
  HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */
  0x00,
  HID_FS_BINTERVAL,          /*bInterval: Polling Interval (10 ms)*/
  /* 34 */
} ;
 

................................

__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE]  __ALIGN_END =
{
     0x05,   0x01,
  0x09,   0x02,
  0xA1,   0x01,
  0x09,   0x01,
  
  0xA1,   0x00,
  0x05,   0x09,
  0x19,   0x01,
  0x29,   0x03,
  
  0x15,   0x00,
  0x25,   0x01,
  0x95,   0x03,
  0x75,   0x01,
  
  0x81,   0x02,
  0x95,   0x01,
  0x75,   0x05,
  0x81,   0x01,
  
  0x05,   0x01,
  0x09,   0x30,
  0x09,   0x31,
  0x09,   0x38,
  
  0x15,   0x81,
  0x25,   0x7F,
  0x75,   0x08,
  0x95,   0x03,
  
  0x81,   0x06,
  0xC0,   0x09,
  0x3c,   0x05,
  0xff,   0x09,
  
  0x01,   0x15,
  0x00,   0x25,
  0x01,   0x75,
  0x01,   0x95,
  
  0x02,   0xb1,
  0x22,   0x75,
  0x06,   0x95,
  0x01,   0xb1,
  
  0x01,   0xc0

};  


 And in Src/main.c add

#include "stm32f7xx_hal.h"
#include "usb_device.h"
#include "usbd_hid.h"
#include <string.h>
#include "stm32f7xx_hal_uart.h"
#include "stm32f7xx_hal_sdram.h"
#include "stm32f7xx_hal_ltdc.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_ts.h"
#include "stm32746g_discovery_lcd.h"
#include "stm32746g_discovery_sdram.h"
#include "stm32f7xx_ll_fmc.h"
#include "stdio.h"
#include <stdbool.h>


void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);

TS_StateTypeDef TS_State;


extern USBD_HandleTypeDef hUsbDeviceHS;
uint8_t buffer[4];


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_USB_DEVICE_Init();
  
      BSP_LCD_Init();
     BSP_LED_Init(LED1);
    BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
    BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*4));
 
    BSP_LCD_DisplayOn();
  
    BSP_LCD_SelectLayer(0);
    BSP_LCD_Clear(LCD_COLOR_BLACK);
    BSP_LCD_SelectLayer(1);
    BSP_LCD_Clear(LCD_COLOR_BLACK);
 
    BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  
    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
    BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);

    if( BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize())!=TS_OK){
         Error_Handler();
    }


    buffer[0]=0x00;
    buffer[1]=0x00;
    buffer[2]=0x00;
    buffer[3]=0x00;
   
    USBD_HID_SendReport(&hUsbDeviceHS,buffer,sizeof(buffer));
   
    BSP_LCD_Clear(LCD_COLOR_WHITE);
    BSP_LCD_DrawRect(130, 40, 200, 120);
    BSP_LCD_DrawRect(130, 180, 100, 60);
    BSP_LCD_DrawRect(230, 180, 100, 60);
    BSP_LCD_DrawRect(360, 40, 30, 120);
    int x_touch,y_touch,pre_x_touch,pre_y_touch;
    //bool mouse_ok=true;
    int mouse_ok=0;
    BSP_LED_On(LED1);

  while (1)
  {
     BSP_TS_GetState(&TS_State);
     if (TS_State.touchDetected==1) {
        x_touch=TS_State.touchX[0];
        y_touch=TS_State.touchY[0];
       
        if(mouse_ok==0){
           
            pre_x_touch=x_touch;
            pre_y_touch=y_touch;
        }
        else if(mouse_ok==1){
           
            if((x_touch>130&&x_touch<330)&&(y_touch>40&&y_touch<160)){
                    buffer[1]=3*(x_touch-pre_x_touch);
                    buffer[2]=3*(y_touch-pre_y_touch);
            }
            else if((x_touch>130&&x_touch<230)&&(y_touch>180&&y_touch<240)){
                buffer[0]=0x01;
                BSP_LED_Off(LED1);
            }
            else if((x_touch>230&&x_touch<330)&&(y_touch>180&&y_touch<240)){
                    buffer[0]=0x02;
           
            }
            else if((x_touch>360&&x_touch<400)&&(y_touch>40&&y_touch<160)){
                    buffer[3]=(int)((y_touch-pre_y_touch)/4);
           
            }
            USBD_HID_SendReport(&hUsbDeviceHS,buffer,sizeof(buffer));
           
        }
        mouse_ok=1;
        pre_x_touch=x_touch;
        pre_y_touch=y_touch;
           
     }
    else if(TS_State.touchDetected==0){
        mouse_ok=0;
        BSP_LED_On(LED1);
    }
    USBD_HID_SendReport(&hUsbDeviceHS,buffer,sizeof(buffer));
    HAL_Delay(40);   
    buffer[0]=0x00;
    buffer[1]=0x00;
    buffer[2]=0x00;
    buffer[3]=0x00;
  }

  /* USER CODE END 3 */


-----------------------------------------------------------------------------------------------