cancel
Showing results for 
Search instead for 
Did you mean: 

USB HID Keyboard LEDs

zg
Associate
Posted on June 13, 2014 at 07:33

Finally find the way out of the Keyboard LEDs working. I use STM32F4 disc with STM32Cube_FW_F4_V1.1.0 and STM32CubeMX 4.2

The HID keyboard program is modified from HID Mouse program
  • Update the usbd_hid.c


...

static uint8_t USBD_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);

...


USBD_ClassTypeDef USBD_HID = 

{

USBD_HID_Init,

USBD_HID_DeInit,

USBD_HID_Setup,

NULL, /*EP0_TxSent*/ 

NULL, /*EP0_RxReady*/

USBD_HID_DataIn, /*DataIn*/

USBD_HID_DataOut, /*DataOut*/


NULL, /*SOF */

NULL,

NULL, 

USBD_HID_GetCfgDesc,

USBD_HID_GetCfgDesc, 

USBD_HID_GetCfgDesc,

USBD_HID_GetDeviceQualifierDesc,

};


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

0x02, /*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,

0x21, /*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: 8 Byte max */

0x00,

0x0A, /*bInterval: Polling Interval (10 ms)*/


/* 34 */

0x07, /*bLength: Endpoint Descriptor size*/

USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/


HID_EPOUT_ADDR, /*bEndpointAddress: Endpoint Address (OUT)*/

0x03, /*bmAttributes: Interrupt endpoint*/

HID_EPOUT_SIZE, /*wMaxPacketSize: 1 Byte max */

0x00,

0x0A, /*bInterval: Polling Interval (10 ms)*/

/* 41 */

} ;


...


__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END ={

0x05, 0x01, // USAGE_PAGE (Generic Desktop)

0x09, 0x06, // USAGE_PAGE (Keyboard)

0xa1, 0x01, // COLLECTION (Application)

0x05, 0x07, // USAGE_PAGE (Keyboard)

0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)

0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)

0x15, 0x00, // LOGICAL_MINIMUM (0)

0x25, 0x01, // LOGICAL_MAXIMUM (1)

0x75, 0x01, // REPORT_SIZE (1)

0x95, 0x08, // REPORT_COUNT (8)

0x81, 0x02, // INPUT (Data,Var,Abs)

0x75, 0x08, // REPORT_SIZE (8)

0x95, 0x01, // REPORT_COUNT (1)

0x81, 0x01, // INPUT (Cnst,Ary,Abs)

0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))

0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)

0x15, 0x00, // LOGICAL_MINIMUM (0)

0x25, 0x65, // LOGICAL_MAXIMUM (101)

0x75, 0x08, // REPORT_SIZE (8)

0x95, 0x06, // REPORT_COUNT (6)

0x81, 0x00, // INPUT (Data,Ary,Abs)

0x05, 0x08, // USAGE_PAGE (LEDs)

0x19, 0x01, // USAGE_MINIMUM (Num Lock)

0x29, 0x03, // USAGE_MAXIMUM (Scroll Lock)

0x15, 0x00, // LOGICAL_MINIMUM (0)

0x25, 0x01, // LOGICAL_MAXIMUM (1)

0x75, 0x01, // REPORT_SIZE (1)

0x95, 0x03, // REPORT_COUNT (3)

0x91, 0x02, // OUTPUT (Data,Var,Abs)

0x75, 0x05, // REPORT_SIZE (5)

0x95, 0x01, // REPORT_COUNT (1)

0x91, 0x03, // OUTPUT (Cnst,Var,Abs)

0xc0 // END_COLLECTION

};


...


static uint8_t rx_buf;


static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev, 

uint8_t cfgidx)

{

uint8_t ret = 0;


/* Open EP IN */

USBD_LL_OpenEP(pdev,

HID_EPIN_ADDR,

USBD_EP_TYPE_INTR,

HID_EPIN_SIZE);


/* Open EP OUT */

USBD_LL_OpenEP(pdev, HID_EPOUT_ADDR, USBD_EP_TYPE_INTR, HID_EPOUT_SIZE);



pdev->pClassData = USBD_malloc(sizeof (USBD_HID_HandleTypeDef));


//set EP_OUT 1 prepared to received the data

USBD_LL_PrepareReceive(pdev, HID_EPOUT_ADDR, &rx_buf, 1);





if(pdev->pClassData == NULL)

{

ret = 1; 

}

else

{

((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE;

}

return ret;

}


...


static uint8_t USBD_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum){


HAL_PCD_EP_Receive(hUsbDeviceFS.pData, HID_EPOUT_ADDR, &rx_buf, 1);


if (rx_buf & 0x01){

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);

}

else{

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);

}


if (rx_buf & 0x02){

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);

}

else{

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);

}


if (rx_buf & 0x04){

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);

}

else{

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);

}

return USBD_OK;

}

  • now update the usbd_hid.h


#define HID_EPIN_ADDR 0x81

//#define HID_EPIN_SIZE 0x04

#define HID_EPIN_SIZE 0x08


#define HID_EPOUT_ADDR 0x01

#define HID_EPOUT_SIZE 0x01


//#define USB_HID_CONFIG_DESC_SIZ 34

#define USB_HID_CONFIG_DESC_SIZ 41

#define USB_HID_DESC_SIZ 9

//#define HID_MOUSE_REPORT_DESC_SIZE 74

#define HID_MOUSE_REPORT_DESC_SIZE 65


#define HID_DESCRIPTOR_TYPE 0x21

#define HID_REPORT_DESC 0x22

Hopefully this could give you a help! #stm32f4 #usb #discovery
2 REPLIES 2
istiaq2379
Associate
Posted on November 06, 2014 at 11:16

Hello leo,

I also tried with the code. But it didn't work. Can you please help me out with some suggestion? Thank you. 

earth75
Associate

Thanks a lot for sharing!

Here I am10 years after you, struggling with the exact same issue and you helped me greatly with your code snippet.

 

For those who stumble here my error was that I forgot to set the number of endpoints to 2 in the interface part of the config descriptor:

0x02, /*bNumEndpoints*/

 Also, do not put too much processing in much processing in the USBD_HID_DataOut function.

I chose to only write data to rx_buf and process it elsewhere.

 

Cheers!