usbd_hid_core.c
Go to the documentation of this file.
1 
49 /* Includes ------------------------------------------------------------------*/
50 #include "usbd_hid_core.h"
51 #include "usbd_desc.h"
52 #include "usbd_req.h"
53 
54 #include "common/utils.h"
55 
56 
99 uint8_t USBD_HID_Init (void *pdev,
100  uint8_t cfgidx);
101 
102 uint8_t USBD_HID_DeInit (void *pdev,
103  uint8_t cfgidx);
104 
105 uint8_t USBD_HID_Setup (void *pdev,
106  USB_SETUP_REQ *req);
107 
108 static uint8_t *USBD_HID_GetCfgDesc (uint8_t speed, uint16_t *length);
109 
110 uint8_t USBD_HID_DataIn (void *pdev, uint8_t epnum);
120 {
124  NULL, /*EP0_TxSent*/
125  NULL, /*EP0_RxReady*/
126  USBD_HID_DataIn, /*DataIn*/
127  NULL, /*DataOut*/
128  NULL, /*SOF */
129  NULL,
130  NULL,
132 #ifdef USB_OTG_HS_CORE
133  USBD_HID_GetCfgDesc, /* use same config as per FS */
134 #endif
135 };
136 
137 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
138  #if defined ( __ICCARM__ )
139  #pragma data_alignment=4
140  #endif
141 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
142 __ALIGN_BEGIN static uint32_t USBD_HID_AltSet __ALIGN_END = 0;
143 
144 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
145  #if defined ( __ICCARM__ )
146  #pragma data_alignment=4
147  #endif
148 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
149 __ALIGN_BEGIN static uint32_t USBD_HID_Protocol __ALIGN_END = 0;
150 
151 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
152  #if defined ( __ICCARM__ )
153  #pragma data_alignment=4
154  #endif
155 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
156 __ALIGN_BEGIN static uint32_t USBD_HID_IdleState __ALIGN_END = 0;
157 
158 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
159  #if defined ( __ICCARM__ )
160  #pragma data_alignment=4
161  #endif
162 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
163 /* USB HID device Configuration Descriptor */
164 __ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END =
165 {
166  0x09, /* bLength: Configuration Descriptor size */
167  USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
169  /* wTotalLength: Bytes returned */
170  0x00,
171  0x01, /*bNumInterfaces: 1 interface*/
172  0x01, /*bConfigurationValue: Configuration value*/
173  0x00, /*iConfiguration: Index of string descriptor describing
174  the configuration*/
175  0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */
176  0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/
177 
178  /************** Descriptor of Joystick Mouse interface ****************/
179  /* 09 */
180  0x09, /*bLength: Interface Descriptor size*/
181  USB_INTERFACE_DESCRIPTOR_TYPE,/*bDescriptorType: Interface descriptor type*/
182  0x00, /*bInterfaceNumber: Number of Interface*/
183  0x00, /*bAlternateSetting: Alternate setting*/
184  0x01, /*bNumEndpoints*/
185  0x03, /*bInterfaceClass: HID*/
186  0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
187  0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
188  0, /*iInterface: Index of string descriptor*/
189  /******************** Descriptor of Joystick Mouse HID ********************/
190  /* 18 */
191  0x09, /*bLength: HID Descriptor size*/
192  HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
193  0x11, /*bcdHID: HID Class Spec release number*/
194  0x01,
195  0x00, /*bCountryCode: Hardware target country*/
196  0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
197  0x22, /*bDescriptorType*/
198  HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
199  0x00,
200  /******************** Descriptor of Mouse endpoint ********************/
201  /* 27 */
202  0x07, /*bLength: Endpoint Descriptor size*/
203  USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/
204 
205  HID_IN_EP, /*bEndpointAddress: Endpoint Address (IN)*/
206  0x03, /*bmAttributes: Interrupt endpoint*/
207  HID_IN_PACKET, /*wMaxPacketSize: 4 Byte max */
208  0x00,
209  HID_FS_BINTERVAL, /*bInterval: Polling Interval (10 ms)*/
210  /* 34 */
211 } ;
212 
213 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
214  #if defined ( __ICCARM__ )
215  #pragma data_alignment=4
216  #endif
217 /* USB HID device Configuration Descriptor */
218 __ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END=
219 {
220  /* 18 */
221  0x09, /*bLength: HID Descriptor size*/
222  HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
223  0x11, /*bcdHID: HID Class Spec release number*/
224  0x01,
225  0x00, /*bCountryCode: Hardware target country*/
226  0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
227  0x22, /*bDescriptorType*/
228  HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
229  0x00,
230 };
231 #endif
232 
233 
234 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
235  #if defined ( __ICCARM__ )
236  #pragma data_alignment=4
237  #endif
238 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
239 __ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END =
240 {
241 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
242 0x09, 0x05, // USAGE (Game Pad)
243 0xa1, 0x01, // COLLECTION (Application)
244 0xa1, 0x00, // COLLECTION (Physical)
245 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
246 0x09, 0x30, // USAGE (X)
247 0x09, 0x31, // USAGE (Y)
248 0x09, 0x32, // USAGE (Z)
249 0x09, 0x33, // USAGE (Rx)
250 0x09, 0x35, // USAGE (Rz)
251 0x09, 0x34, // USAGE (Ry)
252 0x09, 0x40, // USAGE (Vx)
253 0x09, 0x38, // USAGE (Wheel)
254 0x15, 0x81, // LOGICAL_MINIMUM (-127)
255 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
256 0x75, 0x08, // REPORT_SIZE (8)
257 0x95, 0x08, // REPORT_COUNT (8)
258 0x81, 0x02, // INPUT (Data,Var,Abs)
259 0xc0, // END_COLLECTION
260 0xc0 /* END_COLLECTION */
261 };
262 
278 uint8_t USBD_HID_Init (void *pdev,
279  uint8_t cfgidx)
280 {
281 
282  UNUSED(cfgidx);
283  /* Open EP IN */
284  DCD_EP_Open(pdev,
285  HID_IN_EP,
288 
289  return USBD_OK;
290 }
291 
299 uint8_t USBD_HID_DeInit (void *pdev,
300  uint8_t cfgidx)
301 {
302  UNUSED(cfgidx);
303  /* Close HID EPs */
304  DCD_EP_Close (pdev , HID_IN_EP);
305  return USBD_OK;
306 }
307 
315 uint8_t USBD_HID_Setup (void *pdev,
316  USB_SETUP_REQ *req)
317 {
318  uint16_t len = 0;
319  uint8_t *pbuf = NULL;
320 
321  switch (req->bmRequest & USB_REQ_TYPE_MASK)
322  {
323  case USB_REQ_TYPE_CLASS :
324  switch (req->bRequest)
325  {
327  USBD_HID_Protocol = (uint8_t)(req->wValue);
328  break;
329 
331  USBD_CtlSendData (pdev,
332  (uint8_t *)&USBD_HID_Protocol,
333  1);
334  break;
335 
336  case HID_REQ_SET_IDLE:
337  USBD_HID_IdleState = (uint8_t)(req->wValue >> 8);
338  break;
339 
340  case HID_REQ_GET_IDLE:
341  USBD_CtlSendData (pdev,
342  (uint8_t *)&USBD_HID_IdleState,
343  1);
344  break;
345 
346  default:
347  USBD_CtlError (pdev, req);
348  return USBD_FAIL;
349  }
350  break;
351 
353  switch (req->bRequest)
354  {
356  if( req->wValue >> 8 == HID_REPORT_DESC)
357  {
358  len = MIN(HID_MOUSE_REPORT_DESC_SIZE , req->wLength);
359  pbuf = HID_MOUSE_ReportDesc;
360  }
361  else if( req->wValue >> 8 == HID_DESCRIPTOR_TYPE)
362  {
363 
364 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
365  pbuf = USBD_HID_Desc;
366 #else
367  pbuf = USBD_HID_CfgDesc + 0x12;
368 #endif
369  len = MIN(USB_HID_DESC_SIZ , req->wLength);
370  }
371  else
372  {
373  /* Do Nothing */
374  }
375 
376  USBD_CtlSendData (pdev,
377  pbuf,
378  len);
379 
380  break;
381 
382  case USB_REQ_GET_INTERFACE :
383  USBD_CtlSendData (pdev,
384  (uint8_t *)&USBD_HID_AltSet,
385  1);
386  break;
387 
388  case USB_REQ_SET_INTERFACE :
389  USBD_HID_AltSet = (uint8_t)(req->wValue);
390  break;
391 
392  default:
393  USBD_HID_AltSet = (uint8_t)(req->wValue);
394  break;
395  }
396  break;
397 
398  default:
399  USBD_CtlSendData (pdev,
400  (uint8_t *)&USBD_HID_AltSet,
401  1);
402  break;
403  }
404  return USBD_OK;
405 }
406 
415  uint8_t *report,
416  uint16_t len)
417 {
418  if (pdev->dev.device_status == USB_OTG_CONFIGURED )
419  {
420  DCD_EP_Tx (pdev, HID_IN_EP, report, len);
421  }
422  return USBD_OK;
423 }
424 
432 static uint8_t *USBD_HID_GetCfgDesc (uint8_t speed, uint16_t *length)
433 {
434  UNUSED(speed);
435  *length = sizeof (USBD_HID_CfgDesc);
436  return USBD_HID_CfgDesc;
437 }
438 
446 uint8_t USBD_HID_DataIn (void *pdev,
447  uint8_t epnum)
448 {
449 
450  /* Ensure that the FIFO is empty before a new transfer, this condition could
451  be caused by a new transfer before the end of the previous transfer */
452  UNUSED(epnum);
453  DCD_EP_Flush(pdev, HID_IN_EP);
454  return USBD_OK;
455 }
456 
464 {
465  uint32_t polling_interval = 0;
466 
467  /* HIGH-speed endpoints */
468  if(pdev->cfg.speed == USB_OTG_SPEED_HIGH)
469  {
470  /* Sets the data transfer polling interval for high speed transfers.
471  Values between 1..16 are allowed. Values correspond to interval
472  of 2 ^ (bInterval-1). This option (8 ms, corresponds to HID_HS_BINTERVAL */
473  polling_interval = (((1 <<(HID_HS_BINTERVAL - 1)))/8);
474  }
475  else /* LOW and FULL-speed endpoints */
476  {
477  /* Sets the data transfer polling interval for low and full
478  speed transfers */
479  polling_interval = HID_FS_BINTERVAL;
480  }
481 
482  return ((uint32_t)(polling_interval));
483 }
484 
499 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
void USBD_CtlError(USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req)
USBD_CtlError Handle USB low level Error.
Definition: usbd_req.c:813
#define USB_REQ_TYPE_STANDARD
Definition: usbd_def.h:67
header file for the usbd_req.c file
#define USB_HID_DESC_SIZ
Definition: usbd_hid_core.h:49
uint32_t DCD_EP_Tx(USB_OTG_CORE_HANDLE *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t buf_len)
Transmit data over USB.
Definition: usb_dcd.c:271
uint32_t DCD_EP_Close(USB_OTG_CORE_HANDLE *pdev, uint8_t ep_addr)
called when an EP is disabled
Definition: usb_dcd.c:204
uint8_t USBD_HID_Setup(void *pdev, USB_SETUP_REQ *req)
USBD_HID_Setup Handle the HID specific requests.
#define USB_REQ_TYPE_CLASS
Definition: usbd_def.h:68
USBD_Status USBD_CtlSendData(USB_OTG_CORE_HANDLE *pdev, uint8_t *buf, uint16_t len)
USBD_CtlSendData send data on the ctl pipe.
Definition: usbd_ioreq.c:95
#define HID_REQ_SET_IDLE
Definition: usbd_hid_core.h:62
#define USB_REQ_SET_INTERFACE
Definition: usbd_def.h:86
#define USB_REQ_GET_INTERFACE
Definition: usbd_def.h:85
#define USB_OTG_EP_INT
Definition: usb_dcd.h:52
uint32_t DCD_EP_Flush(USB_OTG_CORE_HANDLE *pdev, uint8_t epnum)
This Function flushes the FIFOs.
Definition: usb_dcd.c:360
static uint8_t * USBD_HID_GetCfgDesc(uint8_t speed, uint16_t *length)
USBD_HID_GetCfgDesc return configuration descriptor.
#define HID_HS_BINTERVAL
Definition: usbd_hid_core.h:56
#define HID_REQ_GET_IDLE
Definition: usbd_hid_core.h:63
USBD_Class_cb_TypeDef USBD_HID_cb
header file for the usbd_hid_core.c file.
#define UNUSED(x)
Definition: usbd_msc_bot.c:34
USB_OTG_CORE_CFGS cfg
Definition: usb_core.h:289
#define MIN(a, b)
Definition: usb_defines.h:178
#define HID_REQ_SET_PROTOCOL
Definition: usbd_hid_core.h:59
#define USB_CONFIGURATION_DESCRIPTOR_TYPE
Definition: usbd_cdc_core.h:56
uint8_t speed
Definition: usb_core.h:166
#define USB_OTG_SPEED_HIGH
Definition: usb_defines.h:61
uint8_t USBD_HID_DeInit(void *pdev, uint8_t cfgidx)
USBD_HID_Init DeInitialize the HID layer.
uint32_t DCD_EP_Open(USB_OTG_CORE_HANDLE *pdev, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
Configure an EP.
Definition: usb_dcd.c:165
#define __ALIGN_BEGIN
#define USB_INTERFACE_DESCRIPTOR_TYPE
Definition: usbd_cdc_core.h:58
#define USB_REQ_GET_DESCRIPTOR
Definition: usbd_def.h:81
uint8_t bmRequest
Definition: usb_core.h:182
uint8_t USBD_HID_Init(void *pdev, uint8_t cfgidx)
USBD_HID_Init Initialize the HID interface.
uint8_t USBD_HID_DataIn(void *pdev, uint8_t epnum)
USBD_HID_DataIn handle data IN Stage.
static __ALIGN_BEGIN uint32_t USBD_HID_AltSet __ALIGN_END
#define HID_REQ_GET_PROTOCOL
Definition: usbd_hid_core.h:60
uint8_t USBD_HID_SendReport(USB_OTG_CORE_HANDLE *pdev, uint8_t *report, uint16_t len)
USBD_HID_SendReport Send HID Report.
#define USB_HID_CONFIG_DESC_SIZ
Definition: usbd_hid_core.h:48
#define HID_DESCRIPTOR_TYPE
Definition: usbd_hid_core.h:53
uint32_t USBD_HID_GetPollingInterval(USB_OTG_CORE_HANDLE *pdev)
USBD_HID_GetPollingInterval return polling interval from endpoint descriptor.
uint16_t wLength
Definition: usb_core.h:186
uint8_t bRequest
Definition: usb_core.h:183
#define NULL
Definition: usbd_def.h:50
#define HID_FS_BINTERVAL
Definition: usbd_hid_core.h:57
#define USB_ENDPOINT_DESCRIPTOR_TYPE
Definition: usbd_cdc_core.h:59
#define HID_REPORT_DESC
Definition: usbd_hid_core.h:54
#define HID_MOUSE_REPORT_DESC_SIZE
Definition: usbd_hid_core.h:51
#define USB_OTG_CONFIGURED
Definition: usb_dcd.h:58
#define HID_IN_PACKET
Definition: usbd_conf.h:44
#define USB_REQ_TYPE_MASK
Definition: usbd_def.h:70
#define HID_IN_EP
Definition: usbd_conf.h:43
uint16_t wValue
Definition: usb_core.h:184
header file for the usbd_desc.c file


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:50