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

GD32F103 Firmware

usbd_hid_core.c
Go to the documentation of this file.
1 
11 /* Includes ------------------------------------------------------------------ */
12 #include "usbd_hid_core.h"
13 
29 static uint8_t USBD_HID_Init (void *pudev, uint8_t ConfigIndex);
30 static uint8_t USBD_HID_DeInit (void *pudev, uint8_t ConfigIndex);
31 static uint8_t USBD_HID_GetClassDescriptor (void *pudev, USB_DEVICE_REQ *req);
32 static uint8_t USBD_HID_ClassReqHandle (void *pudev, USB_DEVICE_REQ *req);
33 static uint8_t USBD_HID_GetInterface (void *pudev, USB_DEVICE_REQ *req);
34 static uint8_t USBD_HID_SetInterface (void *pudev, USB_DEVICE_REQ *req);
35 static uint8_t* USBD_HID_GetCfgDesc (uint8_t USBSpeed, uint16_t *len);
36 
40 static uint32_t USBD_HID_AltSet = 0;
41 static uint32_t USBD_HID_Protocol = 0;
42 static uint32_t USBD_HID_IdleState = 0;
43 
51 USBD_Class_cb_TypeDef USBD_HID_cb =
52 {
53  USBD_HID_Init,
54  USBD_HID_DeInit,
55  USBD_HID_GetClassDescriptor,
56  USBD_HID_ClassReqHandle,
57  USBD_HID_GetInterface,
58  USBD_HID_SetInterface,
59  NULL, /* EP0_TxSent */
60  NULL, /* EP0_RxReady */
61  NULL, /* DataIn */
62  NULL, /* DataOut */
63  NULL, /* SOF */
64  USBD_HID_GetCfgDesc,
65 };
66 
67 /* USB HID device configuration descriptor set */
68 const uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZE] =
69 {
70  0x09, /* bLength: configuration descriptor size */
71  USB_DESCTYPE_CONFIGURATION, /* bDescriptorType: configuration descriptor type */
72  USB_HID_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  0xA0, /* bmAttributes: device attributes (bus powered and support remote wakeup) */
78  0x32, /* bMaxPower 100 mA: this current is used for detecting Vbus */
79 
80  /************** 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  0x01, /* bNumEndpoints: just use 1 endpoint for Tx */
86  0x03, /* bInterfaceClass: HID class */
87  0x01, /* bInterfaceSubClass: 0 = no boot, 1 = BIOS boot */
88  0x02, /* nInterfaceProtocol: 0 = none, 1 = keyboard, 2 = mouse */
89  0x00, /* iInterface: index of interface string descriptor */
90 
91  /******************** HID descriptor ********************/
92  0x09, /* bLength: HID descriptor size */
93  HID_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  USB_HID_REPORT_DESC_SIZE, /* wDescriptorLength: total length of report descriptor */
100  0x00,
101 
102  /******************** Mouse endpoint descriptor ********************/
103  0x07, /* bLength: Endpoint Descriptor size */
104  USB_DESCTYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
105  HID_IN_EP, /* bEndpointAddress: endpoint address (EP1_IN) */
106  0x03, /* bmAttributes: endpoint attribute(interrupt endpoint) */
107  HID_IN_PACKET, /* wMaxPacketSize: 4 bytes max */
108  0x00,
109  0x0A, /* bInterval: polling interval (10 ms) */
110 };
111 
112 const uint8_t HID_ReportDesc[USB_HID_REPORT_DESC_SIZE] =
113 {
114  0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
115  0x09, 0x02, /* USAGE (Mouse) */
116  0xA1, 0x01, /* COLLECTION (Application) */
117 
118  0x09, 0x30, /* USAGE (X) */
119  0x09, 0x31, /* USAGE (Y) */
120 
121  0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
122  0x25, 0x7F, /* LOGICAL_MAXIMUM (127) */
123  0x75, 0x08, /* REPORT_SIZE (8) */
124  0x95, 0x02, /* REPORT_COUNT (2) */
125 
126  0x81, 0x06, /* INPUT (Data,Var,Rel) */
127  0xc0, /* END_COLLECTION */
128 };
129 
144 static uint8_t USBD_HID_Init (void *pudev, uint8_t ConfigIndex)
145 {
146  USB_EP_BufConfig(pudev, HID_IN_EP, USB_SNG_BUFTYPE, HID_TX_ADDRESS);
147 
148  /* Initialize Tx endpoint */
149  USB_EP_Init(pudev,
150  HID_IN_EP,
151  USB_EPTYPE_INT,
152  HID_IN_PACKET);
153 
154  /* Initialize Rx endpoint */
155  USB_EP_Init(pudev,
156  HID_OUT_EP,
157  USB_EPTYPE_INT,
158  HID_OUT_PACKET);
159 
160  return USBD_OK;
161 }
162 
169 static uint8_t USBD_HID_DeInit (void *pudev, uint8_t ConfigIndex)
170 {
171  /* Deinitialize HID endpoints */
172  USB_EP_DeInit (pudev, HID_IN_EP);
173  USB_EP_DeInit (pudev, HID_OUT_EP);
174 
175  return USBD_OK;
176 }
177 
184 static uint8_t USBD_HID_ClassReqHandle (void *pudev, USB_DEVICE_REQ *req)
185 {
186  switch (req->bRequest)
187  {
188  case GET_REPORT:
189  /* No use for this driver */
190  break;
191 
192  case GET_IDLE:
193  USB_CtlTx (pudev, (uint8_t *)&USBD_HID_IdleState, 1);
194  break;
195 
196  case GET_PROTOCOL:
197  USB_CtlTx (pudev, (uint8_t *)&USBD_HID_Protocol, 1);
198  break;
199 
200  case SET_REPORT:
201  /* No use for this driver */
202  break;
203 
204  case SET_IDLE:
205  USBD_HID_IdleState = (uint8_t)(req->wValue >> 8);
206  break;
207 
208  case SET_PROTOCOL:
209  USBD_HID_Protocol = (uint8_t)(req->wValue);
210  break;
211 
212  default:
213  USBD_EnumError (pudev, req);
214  return USBD_FAIL;
215  }
216 
217  return USBD_OK;
218 }
219 
226 static uint8_t USBD_HID_GetClassDescriptor (void *pudev, USB_DEVICE_REQ *req)
227 {
228  uint16_t len = 0;
229  uint8_t *pbuf = NULL;
230 
231  switch(req->wValue >> 8)
232  {
233  case HID_REPORT_DESCTYPE:
234  len = MIN(USB_HID_REPORT_DESC_SIZE, req->wLength);
235  pbuf = (uint8_t *)HID_ReportDesc;
236  break;
237 
238  case HID_DESC_TYPE:
239  len = MIN(USB_HID_DESC_SIZE, req->wLength);
240  pbuf = (uint8_t *)USBD_HID_CfgDesc + 0x12;
241  break;
242 
243  default:
244  break;
245  }
246 
247  USB_CtlTx (pudev, pbuf, len);
248 
249  return USBD_OK;
250 }
251 
258 static uint8_t USBD_HID_GetInterface (void *pudev, USB_DEVICE_REQ *req)
259 {
260  USB_CtlTx (pudev, (uint8_t *)&USBD_HID_AltSet, 1);
261 
262  return USBD_OK;
263 }
264 
271 static uint8_t USBD_HID_SetInterface (void *pudev, USB_DEVICE_REQ *req)
272 {
273  USBD_HID_AltSet = (uint8_t)(req->wValue);
274 
275  return USBD_OK;
276 }
277 
286  uint8_t *report,
287  uint16_t Len)
288 {
289  /* Check if USB is configured */
290  if (pudev->dev.device_cur_status == USB_STATUS_CONFIGURED)
291  {
292  USB_EP_Tx (pudev, HID_IN_EP, report, Len);
293  }
294 
295  return USBD_OK;
296 }
297 
304 static uint8_t* USBD_HID_GetCfgDesc (uint8_t USBSpeed, uint16_t *len)
305 {
306  *len = sizeof (USBD_HID_CfgDesc);
307 
308  return (uint8_t *)USBD_HID_CfgDesc;
309 }
310 
331 /************************ (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/
uint8_t USBD_HID_SendReport(USB_DEVICE_HANDLE *pudev, uint8_t *report, uint16_t Len)
Send mouse report.
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
USB mouse (USB HID device) header file.
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_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
Generated on Fri Feb 6 2015 14:56:35 for GD32F10x USB-Device by   doxygen 1.8.8