GD32F10x USB-Device: E:/USB Libraries/GD32_USB_Device_Library/Class/hid/src/usbd_custom_hid_core.c Source File

GD32F103 Firmware

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