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

GD32F1x0

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_DataIn (void *pudev, uint8_t EpID);
36 static uint8_t* USBD_HID_GetCfgDesc (uint8_t USBSpeed, uint16_t *len);
37 
41 static uint32_t USBD_HID_AltSet = 0;
42 static uint32_t USBD_HID_Protocol = 0;
43 static uint32_t USBD_HID_IdleState = 0;
44 
45 extern __IO uint8_t PrevXferComplete;
46 
54 USBD_Class_cb_TypeDef USBD_HID_cb =
55 {
56  USBD_HID_Init,
57  USBD_HID_DeInit,
58  USBD_HID_GetClassDescriptor,
59  USBD_HID_ClassReqHandle,
60  USBD_HID_GetInterface,
61  USBD_HID_SetInterface,
62  NULL, /* EP0_TxSent */
63  NULL, /* EP0_RxReady */
64  USBD_HID_DataIn,
65  NULL, /* DataOut */
66  NULL, /* SOF */
67  USBD_HID_GetCfgDesc,
68 };
69 
70 /* USB HID device configuration descriptor set */
71 const uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZE] =
72 {
73  0x09, /* bLength: configuration descriptor size */
74  USB_DESCTYPE_CONFIGURATION, /* bDescriptorType: configuration descriptor type */
75  USB_HID_CONFIG_DESC_SIZE, /* wTotalLength: configuration descriptor set total length */
76  0x00,
77  0x01, /* bNumInterfaces: 1 interface */
78  0x01, /* bConfigurationValue: configuration value */
79  0x00, /* iConfiguration: index of string descriptor describing the configuration */
80  0xA0, /* bmAttributes: device attributes (bus powered and support remote wakeup) */
81  0x32, /* bMaxPower 100 mA: this current is used for detecting Vbus */
82 
83  /************** interface descriptor ****************/
84  0x09, /* bLength: interface descriptor size */
85  USB_DESCTYPE_INTERFACE,/* bDescriptorType: interface descriptor type */
86  0x00, /* bInterfaceNumber: number of interface */
87  0x00, /* bAlternateSetting: alternate setting */
88  0x01, /* bNumEndpoints: just use 1 endpoint for Tx */
89  0x03, /* bInterfaceClass: HID class */
90  0x01, /* bInterfaceSubClass: 1 = BIOS boot, 0 = no boot */
91  0x02, /* nInterfaceProtocol: 0 = none, 1 = keyboard, 2 = mouse */
92  0x00, /* iInterface: index of interface string descriptor */
93 
94  /******************** HID descriptor ********************/
95  0x09, /* bLength: HID descriptor size */
96  HID_DESC_TYPE,/* bDescriptorType: HID */
97  0x11, /* bcdHID: HID class protocol(HID1.11) */
98  0x01,
99  0x00, /* bCountryCode: device country code */
100  0x01, /* bNumDescriptors: number of HID class descriptors to follow */
101  0x22, /* bDescriptorType: followed class descriptor type(report descriptor) */
102  USB_HID_REPORT_DESC_SIZE, /* wDescriptorLength: total length of report descriptor */
103  0x00,
104 
105  /******************** Mouse endpoint descriptor ********************/
106  0x07, /* bLength: Endpoint Descriptor size */
107  USB_DESCTYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
108  HID_IN_EP, /* bEndpointAddress: endpoint address (EP1_IN) */
109  0x03, /* bmAttributes: endpoint attribute(interrupt endpoint) */
110  HID_IN_PACKET, /* wMaxPacketSize: 4 bytes max */
111  0x00,
112  0x0A, /* bInterval: polling interval (10 ms) */
113 };
114 
115 const uint8_t HID_ReportDesc[USB_HID_REPORT_DESC_SIZE] =
116 {
117  0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
118  0x09, 0x02, /* USAGE (Mouse) */
119  0xA1, 0x01, /* COLLECTION (Application) */
120 
121  0x09, 0x30, /* USAGE (X) */
122  0x09, 0x31, /* USAGE (Y) */
123 
124  0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
125  0x25, 0x7F, /* LOGICAL_MAXIMUM (127) */
126  0x75, 0x08, /* REPORT_SIZE (8) */
127  0x95, 0x02, /* REPORT_COUNT (2) */
128 
129  0x81, 0x06, /* INPUT (Data,Var,Rel) */
130  0xc0, /* END_COLLECTION */
131 };
132 
147 static uint8_t USBD_HID_Init (void *pudev, uint8_t ConfigIndex)
148 {
149  USB_EP_BufConfig(pudev, HID_IN_EP, USB_SNG_BUFTYPE, HID_TX_ADDRESS);
150 
151  /* Initialize Tx endpoint */
152  USB_EP_Init(pudev,
153  HID_IN_EP,
154  USB_EPTYPE_INT,
155  HID_IN_PACKET);
156 
157  /* Initialize Rx endpoint */
158  USB_EP_Init(pudev,
159  HID_OUT_EP,
160  USB_EPTYPE_INT,
161  HID_OUT_PACKET);
162 
163  return USBD_OK;
164 }
165 
172 static uint8_t USBD_HID_DeInit (void *pudev, uint8_t ConfigIndex)
173 {
174  /* Deinitialize HID endpoints */
175  USB_EP_DeInit (pudev, HID_IN_EP);
176  USB_EP_DeInit (pudev, HID_OUT_EP);
177 
178  return USBD_OK;
179 }
180 
187 static uint8_t USBD_HID_ClassReqHandle (void *pudev, USB_DEVICE_REQ *req)
188 {
189  switch (req->bRequest)
190  {
191  case GET_REPORT:
192  /* No use for this driver */
193  break;
194 
195  case GET_IDLE:
196  USB_CtlTx (pudev, (uint8_t *)&USBD_HID_IdleState, 1);
197  break;
198 
199  case GET_PROTOCOL:
200  USB_CtlTx (pudev, (uint8_t *)&USBD_HID_Protocol, 1);
201  break;
202 
203  case SET_REPORT:
204  /* No use for this driver */
205  break;
206 
207  case SET_IDLE:
208  USBD_HID_IdleState = (uint8_t)(req->wValue >> 8);
209  break;
210 
211  case SET_PROTOCOL:
212  USBD_HID_Protocol = (uint8_t)(req->wValue);
213  break;
214 
215  default:
216  USBD_EnumError (pudev, req);
217  return USBD_FAIL;
218  }
219 
220  return USBD_OK;
221 }
222 
229 static uint8_t USBD_HID_GetClassDescriptor (void *pudev, USB_DEVICE_REQ *req)
230 {
231  uint16_t len = 0;
232  uint8_t *pbuf = NULL;
233 
234  switch(req->wValue >> 8)
235  {
236  case HID_REPORT_DESCTYPE:
237  len = MIN(USB_HID_REPORT_DESC_SIZE, req->wLength);
238  pbuf = (uint8_t *)HID_ReportDesc;
239  break;
240 
241  case HID_DESC_TYPE:
242  len = MIN(USB_HID_DESC_SIZE, req->wLength);
243  pbuf = (uint8_t *)USBD_HID_CfgDesc + 0x12;
244  break;
245 
246  default:
247  break;
248  }
249 
250  USB_CtlTx (pudev, pbuf, len);
251 
252  return USBD_OK;
253 }
254 
261 static uint8_t USBD_HID_GetInterface (void *pudev, USB_DEVICE_REQ *req)
262 {
263  USB_CtlTx (pudev, (uint8_t *)&USBD_HID_AltSet, 1);
264 
265  return USBD_OK;
266 }
267 
274 static uint8_t USBD_HID_SetInterface (void *pudev, USB_DEVICE_REQ *req)
275 {
276  USBD_HID_AltSet = (uint8_t)(req->wValue);
277 
278  return USBD_OK;
279 }
280 
289  uint8_t *report,
290  uint16_t Len)
291 {
292  /* Check if USB is configured */
293  if (pudev->dev.device_cur_status == USB_STATUS_CONFIGURED)
294  {
295  PrevXferComplete = 0;
296 
297  USB_EP_Tx (pudev, HID_IN_EP, report, Len);
298  }
299 
300  return USBD_OK;
301 }
302 
308 static uint8_t USBD_HID_DataIn (void *pudev, uint8_t EpID)
309 {
310  PrevXferComplete = 1;
311 
312  return USBD_OK;
313 }
314 
321 static uint8_t* USBD_HID_GetCfgDesc (uint8_t USBSpeed, uint16_t *len)
322 {
323  *len = sizeof (USBD_HID_CfgDesc);
324 
325  return (uint8_t *)USBD_HID_CfgDesc;
326 }
327 
348 /************************ (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: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
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: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_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
Generated by   doxygen 1.8.10