Stm32cubemx HID keyboard

Stm32cubemx HID keyboard . This post is about use HID (Human Interface Device) to send keyboard data from stm32f7 -Disco board to  PC. You can do the same with other boards.

Use stm32cubemx to generate platform source.

To under stand HID interface you can search in google.

1. Pin out setting

RCC-> High speed clock -> Crystal ceramic resonater
Up system clock to 216 Mhz

USB_OTG_FS->Mode->Device Only
Middle wares-> USB_Device-> Human interface device…

 

2. Source generation and edition.

 In /../Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src
edit fine 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*/
  0x01,         /*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, 0x06,
   0xA1, 0x01,
   0x05, 0x07,
   0x19, 0xE0,
   0x29, 0xE7,
    0x15, 0x00,
   0x25, 0x01,
    0x75, 0x01,
   0x95, 0x08,
   0x81, 0x02,
   0x95, 0x01,
   0x75, 0x08,
   0x81, 0x01,
 0x95, 0x03,
 0x75, 0x01,
  0x05, 0x08,
 0x19, 0x01,
 0x29, 0x03,
0x91, 0x02,
 0x95, 0x05,
 0x75, 0x01,
0x91, 0x01,
0x95, 0x06,
  0x75, 0x08,
 0x15, 0x00,
 0x26, 0xFF,
 0x00, 0x05,
0x07, 0x19,
 0x00, 0x2A,
0xFF, 0x00,
 0x81, 0x00,
  0xC0

}; 


and in Src/main.c

#include “stm32f7xx_hal.h”
#include “usb_device.h”
#include “usbd_hid.h”

#define h 0x0b
#define e 0x08
#define l 0x0f
#define o 0x12
#define w 0x1a
#define r 0x15
#define d 0x07
#define space 0x2c
#define enter 0x28

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

extern USBD_HandleTypeDef hUsbDeviceHS;
uint8_t buffer[9];
void keyboard_write(uint8_t char1){
    buffer[2]=char1;
    USBD_HID_SendReport(&hUsbDeviceHS,buffer,sizeof(buffer));
    HAL_Delay(200);
}
 

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();
   

     buffer[0]=0x00;  //to control Ctrl ,Shift ,Alt ….
     buffer[1]=0x00;
    buffer[2]=0x0b;
    buffer[3]=0x00;
    buffer[4]=0x00;
    buffer[5]=0x00;
    buffer[6]=0x00;
    buffer[7]=0x00;
    buffer[8]=0x00;
    USBD_HID_SendReport(&hUsbDeviceHS,buffer,sizeof(buffer));

  while (1)
  {

    keyboard_write(h);
    keyboard_write(e);
    keyboard_write(l);
    keyboard_write(l);
    keyboard_write(o);
    keyboard_write(space);
    keyboard_write(w);
    keyboard_write(o);
    keyboard_write(r);
    keyboard_write(l);
    keyboard_write(d);
    keyboard_write(enter);

  }
  /* USER CODE END 3 */

 

video  Stm32cubemx HID keyboard

 

Sponsored Links: