GD32F1x0: USB/GD32_USB_Device_Library/Class/hid/src/usbd_custom_hid_core.c Source File

GD32F1x0

usbd_custom_hid_core.c
Go to the documentation of this file.
1 
11 /* Includes ------------------------------------------------------------------*/
12 #include "usbd_custom_hid_core.h"
13 
29 static uint8_t USBD_CUSTOMHID_Init (void *pudev, uint8_t ConfigIndex);
30 static uint8_t USBD_CUSTOMHID_DeInit (void *pudev, uint8_t ConfigIndex);
31 static uint8_t USBD_CUSTOMHID_GetClassDescriptor (void *pudev, USB_DEVICE_REQ *req);
32 static uint8_t USBD_CUSTOMHID_ClassReqHandle (void *pudev, USB_DEVICE_REQ *req);
33 static uint8_t USBD_CUSTOMHID_GetInterface (void *pudev, USB_DEVICE_REQ *req);
34 static uint8_t USBD_CUSTOMHID_SetInterface (void *pudev, USB_DEVICE_REQ *req);
35 static uint8_t USBD_CUSTOMHID_EP0_RxReady (void *pudev);
36 static uint8_t USBD_CUSTOMHID_DataIn (void *pudev, uint8_t EpID);
37 static uint8_t USBD_CUSTOMHID_DataOut (void *pudev, uint8_t EpID);
38 static uint8_t* USBD_CUSTOMHID_GetCfgDesc (uint8_t USBSpeed, uint16_t *len);
39 
43 uint8_t Report_buf[2];
44 uint8_t USBD_CUSTOMHID_Report_ID = 0;
45 uint8_t flag = 0;
46 
47 static uint32_t USBD_CUSTOMHID_AltSet = 0;
48 static uint32_t USBD_CUSTOMHID_Protocol = 0;
49 static uint32_t USBD_CUSTOMHID_IdleState = 0;
50 
51 USBD_Class_cb_TypeDef USBD_CUSTOMHID_cb =
52 {
53  USBD_CUSTOMHID_Init,
54  USBD_CUSTOMHID_DeInit,
55  USBD_CUSTOMHID_GetClassDescriptor,
56  USBD_CUSTOMHID_ClassReqHandle,
57  USBD_CUSTOMHID_GetInterface,
58  USBD_CUSTOMHID_SetInterface,
59  NULL, /* EP0_TxSent */
60  USBD_CUSTOMHID_EP0_RxReady,
61  USBD_CUSTOMHID_DataIn,
62  USBD_CUSTOMHID_DataOut,
63  NULL, /* SOF */
64  USBD_CUSTOMHID_GetCfgDesc,
65 };
66 
67 /* USB HID device Configuration Descriptor */
68 const uint8_t USBD_HID_CfgDesc[CUSTOMHID_CONFIG_DESC_SIZE] =
69 {
70  0x09, /* bLength: Configuration Descriptor size */
71  USB_DESCTYPE_CONFIGURATION, /* bDescriptorType: Configuration */
72  CUSTOMHID_CONFIG_DESC_SIZE, /* wTotalLength: configuration descriptor set total length */
73  0x00,
74  0x01, /* bNumInterfaces: 1 interface */
75  0x01, /* bConfigurationValue: configuration value */
76  0x00, /* iConfiguration: index of string descriptor describing the configuration */
77  0x80, /* bmAttributes: device attributes (bus powered and not support remote wakeup) */
78  0x32, /* bMaxPower 100 mA: this current is used for detecting Vbus */
79 
80  /************** Custom HID interface descriptor ****************/
81  0x09, /* bLength: interface descriptor size */
82  USB_DESCTYPE_INTERFACE,/* bDescriptorType: interface descriptor type */
83  0x00, /* bInterfaceNumber: number of interface */
84  0x00, /* bAlternateSetting: alternate setting */
85  0x02, /* bNumEndpoints: 2 endpoints */
86  0x03, /* bInterfaceClass: HID */
87  0x00, /* bInterfaceSubClass: 1 = BIOS boot, 0 = no boot */
88  0x00, /* nInterfaceProtocol: 0 = none, 1 = keyboard, 2 = mouse */
89  0x00, /* iInterface: index of string descriptor */
90 
91  /******************** HID descriptor ********************/
92  0x09, /* bLength: HID descriptor size */
93  CUSTOMHID_DESC_TYPE, /* bDescriptorType: HID */
94  0x11, /* bcdHID: HID class protocol(HID1.11) */
95  0x01,
96  0x00, /* bCountryCode: device country code */
97  0x01, /* bNumDescriptors: number of HID class descriptors to follow */
98  0x22, /* bDescriptorType: followed class descriptor type(report descriptor) */
99  CUSTOMHID_REPORT_DESC_SIZE, /* wDescriptorLength: total length of report descriptor */
100  0x00,
101 
102  /******************** Custom HID endpoints descriptor ***********/
103  0x07, /* bLength: Endpoint Descriptor size */
104  USB_DESCTYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
105  CUSTOMHID_IN_EP, /* bEndpointAddress: endpoint address (IN) */
106  0x03, /* bmAttributes: endpoint attribute(interrupt endpoint) */
107  CUSTOMHID_IN_PACKET, /* wMaxPacketSize: 2 bytes max */
108  0x00,
109  0x20, /* bInterval: Polling Interval (32 ms) */
110 
111  0x07, /* bLength: endpoint descriptor size */
112  USB_DESCTYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
113  CUSTOMHID_OUT_EP, /* bEndpointAddress: endpoint address (OUT) */
114  0x03, /* bmAttributes: endpoint attribute(interrupt endpoint) */
115  CUSTOMHID_OUT_PACKET, /* wMaxPacketSize: 2 Bytes max */
116  0x00,
117  0x20, /* bInterval: Polling Interval (32 ms) */
118 };
119 
120 /* USB custom HID device report descriptor */
121 const uint8_t CustomHID_ReportDescriptor[CUSTOMHID_REPORT_DESC_SIZE] =
122 {
123  0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
124  0x09, 0x00, /* USAGE (Custom Device) */
125  0xa1, 0x01, /* COLLECTION (Application) */
126 
127  /* Led 1 */
128  0x85, 0x11, /* REPORT_ID (0x11) */
129  0x09, 0x01, /* USAGE (LED 1) */
130  0x15, 0x00, /* LOGICAL_MINIMUM (0) */
131  0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
132  0x75, 0x08, /* REPORT_SIZE (8) */
133  0x95, 0x01, /* REPORT_COUNT (1) */
134  0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
135 
136  /* Led 2 */
137  0x85, 0x12, /* REPORT_ID (0x12) */
138  0x09, 0x02, /* USAGE (LED 2) */
139  0x15, 0x00, /* LOGICAL_MINIMUM (0) */
140  0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
141  0x75, 0x08, /* REPORT_SIZE (8) */
142  0x95, 0x01, /* REPORT_COUNT (1) */
143  0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
144 
145  /* Led 3 */
146  0x85, 0x13, /* REPORT_ID (0x13) */
147  0x09, 0x03, /* USAGE (LED 3) */
148  0x15, 0x00, /* LOGICAL_MINIMUM (0) */
149  0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
150  0x75, 0x08, /* REPORT_SIZE (8) */
151  0x95, 0x01, /* REPORT_COUNT (1) */
152  0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
153 
154  /* Led 4 */
155  0x85, 0x14, /* REPORT_ID (0x14) */
156  0x09, 0x04, /* USAGE (LED 4) */
157  0x15, 0x00, /* LOGICAL_MINIMUM (0) */
158  0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
159  0x75, 0x08, /* REPORT_SIZE (8) */
160  0x95, 0x01, /* REPORT_COUNT (1) */
161  0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
162 
163  /* Wakeup Push Button */
164  0x85, 0x15, /* REPORT_ID (0x15) */
165  0x09, 0x05, /* USAGE (Push Button) */
166  0x15, 0x00, /* LOGICAL_MINIMUM (0) */
167  0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
168  0x75, 0x01, /* REPORT_SIZE (1) */
169  0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
170 
171  0x75, 0x07, /* REPORT_SIZE (7) */
172  0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
173 
174  /* Tamper Push Button */
175  0x85, 0x16, /* REPORT_ID (0x16) */
176  0x09, 0x06, /* USAGE (Push Button) */
177  0x15, 0x00, /* LOGICAL_MINIMUM (0) */
178  0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
179  0x75, 0x01, /* REPORT_SIZE (1) */
180  0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
181 
182  0x75, 0x07, /* REPORT_SIZE (7) */
183  0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
184 
185  0xc0 /* END_COLLECTION */
186 };
187 
202 uint8_t USBD_CUSTOMHID_Init (void *pudev, uint8_t ConfigIndex)
203 {
204  USB_EP_BufConfig(pudev, CUSTOMHID_IN_EP, USB_SNG_BUFTYPE, HID_TX_ADDRESS);
205  USB_EP_BufConfig(pudev, CUSTOMHID_OUT_EP, USB_SNG_BUFTYPE, HID_RX_ADDRESS);
206 
207  /* Initialize Tx endpoint */
208  USB_EP_Init(pudev,
209  CUSTOMHID_IN_EP,
210  USB_EPTYPE_INT,
211  CUSTOMHID_IN_PACKET);
212 
213  /* Initialize Rx endpoint */
214  USB_EP_Init(pudev,
215  CUSTOMHID_OUT_EP,
216  USB_EPTYPE_INT,
217  CUSTOMHID_OUT_PACKET);
218 
219  /* Prepare receive Data */
220  USB_EP_Rx(pudev, CUSTOMHID_OUT_EP, Report_buf, 2);
221 
222  return USBD_OK;
223 }
224 
231 uint8_t USBD_CUSTOMHID_DeInit (void *pudev, uint8_t ConfigIndex)
232 {
233  /* Deinitialize the custom HID endpoints */
234  USB_EP_DeInit (pudev, CUSTOMHID_IN_EP);
235  USB_EP_DeInit (pudev, CUSTOMHID_OUT_EP);
236 
237  return USBD_OK;
238 }
239 
246 uint8_t USBD_CUSTOMHID_ClassReqHandle (void *pudev, USB_DEVICE_REQ *req)
247 {
248  uint8_t USBD_CUSTOMHID_Report_LENGTH = 0;
249 
250  switch (req->bRequest)
251  {
252  case GET_REPORT:
253  /* No use for this driver */
254  break;
255 
256  case GET_IDLE:
257  USB_CtlTx (pudev, (uint8_t *)&USBD_CUSTOMHID_IdleState, 1);
258  break;
259 
260  case GET_PROTOCOL:
261  USB_CtlTx (pudev, (uint8_t *)&USBD_CUSTOMHID_Protocol, 1);
262  break;
263 
264  case SET_REPORT:
265  flag = 1;
266  USBD_CUSTOMHID_Report_ID = (uint8_t)(req->wValue);
267  USBD_CUSTOMHID_Report_LENGTH = (uint8_t)(req->wLength);
268  USB_CtlRx (pudev, Report_buf, USBD_CUSTOMHID_Report_LENGTH);
269  break;
270 
271  case SET_IDLE:
272  USBD_CUSTOMHID_IdleState = (uint8_t)(req->wValue >> 8);
273  break;
274 
275  case SET_PROTOCOL:
276  USBD_CUSTOMHID_Protocol = (uint8_t)(req->wValue);
277  break;
278 
279  default:
280  USBD_EnumError (pudev, req);
281  return USBD_FAIL;
282  }
283 
284  return USBD_OK;
285 }
286 
293 uint8_t USBD_CUSTOMHID_GetClassDescriptor (void *pudev, USB_DEVICE_REQ *req)
294 {
295  uint16_t len = 0;
296  uint8_t *pbuf = NULL;
297 
298  switch(req->wValue >> 8)
299  {
300  case CUSTOMHID_REPORT_DESCTYPE:
301  len = MIN(CUSTOMHID_REPORT_DESC_SIZE, req->wLength);
302  pbuf = (uint8_t *)CustomHID_ReportDescriptor;
303  break;
304 
305  case CUSTOMHID_DESC_TYPE:
306  len = MIN(USB_CUSTOMHID_DESC_SIZE, req->wLength);
307  pbuf = (uint8_t *)USBD_HID_CfgDesc + 0x12;
308  break;
309 
310  default:
311  break;
312  }
313 
314  USB_CtlTx (pudev, pbuf, len);
315 
316  return USBD_OK;
317 }
318 
325 uint8_t USBD_CUSTOMHID_GetInterface (void *pudev, USB_DEVICE_REQ *req)
326 {
327  USB_CtlTx (pudev, (uint8_t *)&USBD_CUSTOMHID_AltSet, 1);
328 
329  return USBD_OK;
330 }
331 
338 uint8_t USBD_CUSTOMHID_SetInterface (void *pudev, USB_DEVICE_REQ *req)
339 {
340  USBD_CUSTOMHID_AltSet = (uint8_t)(req->wValue);
341 
342  return USBD_OK;
343 }
344 
353  uint8_t *report,
354  uint16_t Len)
355 {
356  if (pudev->dev.device_cur_status == USB_STATUS_CONFIGURED)
357  {
358  USB_EP_Tx (pudev, CUSTOMHID_IN_EP, report, Len);
359  }
360 
361  return USBD_OK;
362 }
363 
370 uint8_t *USBD_CUSTOMHID_GetCfgDesc (uint8_t USBSpeed, uint16_t *len)
371 {
372  *len = sizeof (USBD_HID_CfgDesc);
373 
374  return (uint8_t*)USBD_HID_CfgDesc;
375 }
376 
383 uint8_t USBD_CUSTOMHID_DataIn (void *pudev, uint8_t EpID)
384 {
385  return USBD_OK;
386 }
387 
394 uint8_t USBD_CUSTOMHID_DataOut (void *pudev, uint8_t EpID)
395 {
396  if (EpID == 1)
397  {
398  switch (Report_buf[0])
399  {
400  case 0x11:
401  if (Report_buf[1] != Bit_RESET)
402  {
403  GD_EVAL_LEDOn(LED1);
404  }
405  else
406  {
407  GD_EVAL_LEDOff(LED1);
408  }
409  break;
410 
411  case 0x12:
412  if (Report_buf[1] != Bit_RESET)
413  {
414  GD_EVAL_LEDOn(LED2);
415  }
416  else
417  {
418  GD_EVAL_LEDOff(LED2);
419  }
420  break;
421 
422  case 0x13:
423  if (Report_buf[1] != Bit_RESET)
424  {
425  GD_EVAL_LEDOn(LED3);
426  }
427  else
428  {
429  GD_EVAL_LEDOff(LED3);
430  }
431  break;
432 
433  case 0x14:
434  if (Report_buf[1] != Bit_RESET)
435  {
436  GD_EVAL_LEDOn(LED4);
437  }
438  else
439  {
440  GD_EVAL_LEDOff(LED4);
441  }
442  break;
443 
444  default:
445  GD_EVAL_LEDOff(LED1);
446  GD_EVAL_LEDOff(LED2);
447  GD_EVAL_LEDOff(LED3);
448  GD_EVAL_LEDOff(LED4);
449  break;
450  }
451  }
452 
453  USB_EP_Rx(pudev, CUSTOMHID_IN_EP, Report_buf, 2);
454 
455  return USBD_OK;
456 }
457 
463 static uint8_t USBD_CUSTOMHID_EP0_RxReady (void *pudev)
464 {
465  if (flag == 1)
466  {
467  flag = 0;
468 
469  switch (Report_buf[0])
470  {
471  case 0x11:
472  if (Report_buf[1] != Bit_RESET)
473  {
474  GD_EVAL_LEDOn(LED1);
475  }
476  else
477  {
478  GD_EVAL_LEDOff(LED1);
479  }
480  break;
481 
482  case 0x12:
483  if (Report_buf[1] != Bit_RESET)
484  {
485  GD_EVAL_LEDOn(LED2);
486  }
487  else
488  {
489  GD_EVAL_LEDOff(LED2);
490  }
491  break;
492 
493  case 0x13:
494  if (Report_buf[1] != Bit_RESET)
495  {
496  GD_EVAL_LEDOn(LED3);
497  }
498  else
499  {
500  GD_EVAL_LEDOff(LED3);
501  }
502  break;
503 
504  case 0x14:
505  if (Report_buf[1] != Bit_RESET)
506  {
507  GD_EVAL_LEDOn(LED4);
508  }
509  else
510  {
511  GD_EVAL_LEDOff(LED4);
512  }
513  break;
514 
515  default:
516  GD_EVAL_LEDOff(LED1);
517  GD_EVAL_LEDOff(LED2);
518  GD_EVAL_LEDOff(LED3);
519  GD_EVAL_LEDOff(LED4);
520  break;
521  }
522  }
523 
524  return USBD_OK;
525 }
526 
547 /************************ (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/
USB device class callback type define.
Definition: usb_core.h:153
void USB_EP_Tx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to transmit data.
Definition: usb_core.c:376
uint8_t USBD_CUSTOMHID_SendReport(USB_DEVICE_HANDLE *pudev, uint8_t *report, uint16_t Len)
Send custom HID Report.
void USB_EP_DeInit(USB_CORE_HANDLE *pudev, uint8_t EpAddr)
Configure the endpoint when it is disabled.
Definition: usb_core.c:275
void USB_EP_Init(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t EpType, uint16_t EpMps)
Endpoint initialization.
Definition: usb_core.c:169
uint8_t USB_CtlRx(USB_CORE_HANDLE *pudev, uint8_t *pbuf, uint16_t Len)
Receive data on the control pipe.
Definition: usb_core.c:577
uint8_t USB_CtlTx(USB_CORE_HANDLE *pudev, uint8_t *pbuf, uint16_t Len)
Transmit data on the control pipe.
Definition: usb_core.c:543
#define USB_SNG_BUFTYPE
USB endpoint kind.
Definition: usb_core.h:52
void USB_EP_BufConfig(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t EpKind, uint32_t BufAddr)
Configure buffer for endpoint.
Definition: usb_core.c:107
void USBD_EnumError(USB_DEVICE_HANDLE *pudev, USB_DEVICE_REQ *req)
Handle usb enumeration error event.
Definition: usbd_enum.c:755
USB standard device request struct.
Definition: usb_core.h:122
void USB_EP_Rx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to receive data.
Definition: usb_core.c:336
Custom HID device class core defines.
Generated by   doxygen 1.8.10