AnsweredAssumed Answered

USB HID Keyboard LEDs

Question asked by liang.leo on Jun 13, 2014
Latest reply on Nov 6, 2014 by mahmud.istiak
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
001....
002.static uint8_t USBD_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
003....
004. 
005.USBD_ClassTypeDef  USBD_HID =
006.{
007.  USBD_HID_Init,
008.  USBD_HID_DeInit,
009.  USBD_HID_Setup,
010.  NULL, /*EP0_TxSent*/ 
011.  NULL, /*EP0_RxReady*/
012.  USBD_HID_DataIn, /*DataIn*/
013.  USBD_HID_DataOut, /*DataOut*/
014.  NULL, /*SOF */
015.  NULL,
016.  NULL,     
017.  USBD_HID_GetCfgDesc,
018.  USBD_HID_GetCfgDesc,
019.  USBD_HID_GetCfgDesc,
020.  USBD_HID_GetDeviceQualifierDesc,
021.};
022. 
023.__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ]  __ALIGN_END =
024.{
025.  0x09, /* bLength: Configuration Descriptor size */
026.  USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
027.  USB_HID_CONFIG_DESC_SIZ,
028.  /* wTotalLength: Bytes returned */
029.  0x00,
030.  0x01,         /*bNumInterfaces: 1 interface*/
031.  0x01,         /*bConfigurationValue: Configuration value*/
032.  0x00,         /*iConfiguration: Index of string descriptor describing
033.  the configuration*/
034.  0xE0,         /*bmAttributes: bus powered and Support Remote Wake-up */
035.  0x32,         /*MaxPower 100 mA: this current is used for detecting Vbus*/
036.   
037.  /************** Descriptor of Joystick Mouse interface ****************/
038.  /* 09 */
039.  0x09,         /*bLength: Interface Descriptor size*/
040.  USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
041.  0x00,         /*bInterfaceNumber: Number of Interface*/
042.  0x00,         /*bAlternateSetting: Alternate setting*/
043.  0x02,         /*bNumEndpoints*/
044.  0x03,         /*bInterfaceClass: HID*/
045.  0x01,         /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
046.  0x01,         /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
047.  0,            /*iInterface: Index of string descriptor*/
048.  /******************** Descriptor of Joystick Mouse HID ********************/
049.  /* 18 */
050.  0x09,         /*bLength: HID Descriptor size*/
051.  HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
052.  0x11,         /*bcdHID: HID Class Spec release number*/
053.  0x01,
054.  0x21,         /*bCountryCode: Hardware target country*/
055.  0x01,         /*bNumDescriptors: Number of HID class descriptors to follow*/
056.  0x22,         /*bDescriptorType*/
057.  HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
058.  0x00,
059.  /******************** Descriptor of Mouse endpoint ********************/
060.  /* 27 */
061.  0x07,          /*bLength: Endpoint Descriptor size*/
062.  USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
063.   
064.  HID_EPIN_ADDR,     /*bEndpointAddress: Endpoint Address (IN)*/
065.  0x03,          /*bmAttributes: Interrupt endpoint*/
066.  HID_EPIN_SIZE, /*wMaxPacketSize: 8 Byte max */
067.  0x00,
068.  0x0A,          /*bInterval: Polling Interval (10 ms)*/
069.   
070.  /* 34 */
071.  0x07,          /*bLength: Endpoint Descriptor size*/
072.  USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
073.   
074.  HID_EPOUT_ADDR,     /*bEndpointAddress: Endpoint Address (OUT)*/
075.  0x03,          /*bmAttributes: Interrupt endpoint*/
076.  HID_EPOUT_SIZE, /*wMaxPacketSize: 1 Byte max */
077.  0x00,
078.  0x0A,          /*bInterval: Polling Interval (10 ms)*/
079.  /* 41 */
080.} ;
081. 
082....
083. 
084.__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE]  __ALIGN_END ={
085.    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
086.    0x09, 0x06,                    // USAGE_PAGE (Keyboard)
087.    0xa1, 0x01,                    // COLLECTION (Application)
088.    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
089.    0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
090.    0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
091.    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
092.    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
093.    0x75, 0x01,                    //   REPORT_SIZE (1)
094.    0x95, 0x08,                    //   REPORT_COUNT (8)
095.    0x81, 0x02,                    //   INPUT (Data,Var,Abs)
096.    0x75, 0x08,                    //   REPORT_SIZE (8)
097.    0x95, 0x01,                    //   REPORT_COUNT (1)
098.    0x81, 0x01,                    //   INPUT (Cnst,Ary,Abs)
099.    0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
100.    0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
101.    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
102.    0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
103.    0x75, 0x08,                    //   REPORT_SIZE (8)
104.    0x95, 0x06,                    //   REPORT_COUNT (6)
105.    0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
106.    0x05, 0x08,                    //   USAGE_PAGE (LEDs)
107.    0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
108.    0x29, 0x03,                    //   USAGE_MAXIMUM (Scroll Lock)
109.    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
110.    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
111.    0x75, 0x01,                    //   REPORT_SIZE (1)
112.    0x95, 0x03,                    //   REPORT_COUNT (3)
113.    0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
114.    0x75, 0x05,                    //   REPORT_SIZE (5)
115.    0x95, 0x01,                    //   REPORT_COUNT (1)
116.    0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
117.    0xc0                           // END_COLLECTION
118.};
119. 
120....
121. 
122.static uint8_t rx_buf;
123. 
124.static uint8_t  USBD_HID_Init (USBD_HandleTypeDef *pdev,
125.                               uint8_t cfgidx)
126.{
127.  uint8_t ret = 0;
128.   
129.  /* Open EP IN */
130.  USBD_LL_OpenEP(pdev,
131.                 HID_EPIN_ADDR,
132.                 USBD_EP_TYPE_INTR,
133.                 HID_EPIN_SIZE);
134.   
135.  /* Open EP OUT */
136.  USBD_LL_OpenEP(pdev, HID_EPOUT_ADDR, USBD_EP_TYPE_INTR, HID_EPOUT_SIZE);
137.   
138.  pdev->pClassData = USBD_malloc(sizeof (USBD_HID_HandleTypeDef));
139.   
140.    //set EP_OUT 1 prepared to received the data
141.    USBD_LL_PrepareReceive(pdev, HID_EPOUT_ADDR, &rx_buf, 1);
142.     
143.     
144.     
145.  if(pdev->pClassData == NULL)
146.  {
147.    ret = 1;
148.  }
149.  else
150.  {
151.    ((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE;
152.  }
153.  return ret;
154.}
155. 
156....
157. 
158.static uint8_t USBD_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum){
159.     
160.    HAL_PCD_EP_Receive(hUsbDeviceFS.pData, HID_EPOUT_ADDR, &rx_buf, 1);
161.     
162.    if (rx_buf & 0x01){
163.        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
164.    }
165.    else{
166.        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
167.    }
168.     
169.    if (rx_buf & 0x02){
170.        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
171.    }
172.    else{
173.        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
174.    }
175.     
176.    if (rx_buf & 0x04){
177.        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
178.    }
179.    else{
180.        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
181.    }
182.    return USBD_OK;
183.}

       
  • now update the usbd_hid.h
01.#define HID_EPIN_ADDR                 0x81
02.//#define HID_EPIN_SIZE                 0x04
03.#define HID_EPIN_SIZE                 0x08
04. 
05.#define HID_EPOUT_ADDR              0x01
06.#define HID_EPOUT_SIZE              0x01
07. 
08.//#define USB_HID_CONFIG_DESC_SIZ       34
09.#define USB_HID_CONFIG_DESC_SIZ         41
10.#define USB_HID_DESC_SIZ              9
11.//#define HID_MOUSE_REPORT_DESC_SIZE    74
12.#define HID_MOUSE_REPORT_DESC_SIZE      65
13. 
14.#define HID_DESCRIPTOR_TYPE           0x21
15.#define HID_REPORT_DESC               0x22

Hopefully this could give you a help!

Outcomes