STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
usbd_customhid.c
Go to the documentation of this file.
1 
48 /* Includes ------------------------------------------------------------------*/
49 #include "usbd_customhid.h"
50 #include "usbd_desc.h"
51 #include "usbd_ctlreq.h"
52 
53 
92 static uint8_t USBD_CUSTOM_HID_Init (USBD_HandleTypeDef *pdev,
93  uint8_t cfgidx);
94 
95 static uint8_t USBD_CUSTOM_HID_DeInit (USBD_HandleTypeDef *pdev,
96  uint8_t cfgidx);
97 
98 static uint8_t USBD_CUSTOM_HID_Setup (USBD_HandleTypeDef *pdev,
100 
101 static uint8_t *USBD_CUSTOM_HID_GetCfgDesc (uint16_t *length);
102 
103 static uint8_t *USBD_CUSTOM_HID_GetDeviceQualifierDesc (uint16_t *length);
104 
105 static uint8_t USBD_CUSTOM_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum);
106 
107 static uint8_t USBD_CUSTOM_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum);
108 static uint8_t USBD_CUSTOM_HID_EP0_RxReady (USBD_HandleTypeDef *pdev);
118 {
119  USBD_CUSTOM_HID_Init,
120  USBD_CUSTOM_HID_DeInit,
121  USBD_CUSTOM_HID_Setup,
122  NULL, /*EP0_TxSent*/
123  USBD_CUSTOM_HID_EP0_RxReady, /*EP0_RxReady*/ /* STATUS STAGE IN */
124  USBD_CUSTOM_HID_DataIn, /*DataIn*/
125  USBD_CUSTOM_HID_DataOut,
126  NULL, /*SOF */
127  NULL,
128  NULL,
129  USBD_CUSTOM_HID_GetCfgDesc,
130  USBD_CUSTOM_HID_GetCfgDesc,
131  USBD_CUSTOM_HID_GetCfgDesc,
132  USBD_CUSTOM_HID_GetDeviceQualifierDesc,
133 };
134 
135 /* USB CUSTOM_HID device Configuration Descriptor */
136 __ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_CfgDesc[USB_CUSTOM_HID_CONFIG_DESC_SIZ] __ALIGN_END =
137 {
138  0x09, /* bLength: Configuration Descriptor size */
139  USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
141  /* wTotalLength: Bytes returned */
142  0x00,
143  0x01, /*bNumInterfaces: 1 interface*/
144  0x01, /*bConfigurationValue: Configuration value*/
145  0x00, /*iConfiguration: Index of string descriptor describing
146  the configuration*/
147  0xC0, /*bmAttributes: bus powered */
148  0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/
149 
150  /************** Descriptor of CUSTOM HID interface ****************/
151  /* 09 */
152  0x09, /*bLength: Interface Descriptor size*/
153  USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
154  0x00, /*bInterfaceNumber: Number of Interface*/
155  0x00, /*bAlternateSetting: Alternate setting*/
156  0x02, /*bNumEndpoints*/
157  0x03, /*bInterfaceClass: CUSTOM_HID*/
158  0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
159  0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
160  0, /*iInterface: Index of string descriptor*/
161  /******************** Descriptor of CUSTOM_HID *************************/
162  /* 18 */
163  0x09, /*bLength: CUSTOM_HID Descriptor size*/
164  CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/
165  0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/
166  0x01,
167  0x00, /*bCountryCode: Hardware target country*/
168  0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/
169  0x22, /*bDescriptorType*/
170  USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
171  0x00,
172  /******************** Descriptor of Custom HID endpoints ********************/
173  /* 27 */
174  0x07, /*bLength: Endpoint Descriptor size*/
175  USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
176 
177  CUSTOM_HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/
178  0x03, /*bmAttributes: Interrupt endpoint*/
179  CUSTOM_HID_EPIN_SIZE, /*wMaxPacketSize: 2 Byte max */
180  0x00,
181  0x20, /*bInterval: Polling Interval (20 ms)*/
182  /* 34 */
183 
184  0x07, /* bLength: Endpoint Descriptor size */
185  USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: */
186  CUSTOM_HID_EPOUT_ADDR, /*bEndpointAddress: Endpoint Address (OUT)*/
187  0x03, /* bmAttributes: Interrupt endpoint */
188  CUSTOM_HID_EPOUT_SIZE, /* wMaxPacketSize: 2 Bytes max */
189  0x00,
190  0x20, /* bInterval: Polling Interval (20 ms) */
191  /* 41 */
192 } ;
193 
194 /* USB CUSTOM_HID device Configuration Descriptor */
195 __ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_Desc[USB_CUSTOM_HID_DESC_SIZ] __ALIGN_END =
196 {
197  /* 18 */
198  0x09, /*bLength: CUSTOM_HID Descriptor size*/
199  CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/
200  0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/
201  0x01,
202  0x00, /*bCountryCode: Hardware target country*/
203  0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/
204  0x22, /*bDescriptorType*/
205  USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
206  0x00,
207 };
208 
209 /* USB Standard Device Descriptor */
210 __ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
211 {
214  0x00,
215  0x02,
216  0x00,
217  0x00,
218  0x00,
219  0x40,
220  0x01,
221  0x00,
222 };
223 
239 static uint8_t USBD_CUSTOM_HID_Init (USBD_HandleTypeDef *pdev,
240  uint8_t cfgidx)
241 {
242  uint8_t ret = 0;
244  /* Open EP IN */
245  USBD_LL_OpenEP(pdev,
249 
250  /* Open EP OUT */
251  USBD_LL_OpenEP(pdev,
255 
257 
258  if(pdev->pClassData == NULL)
259  {
260  ret = 1;
261  }
262  else
263  {
265 
266  hhid->state = CUSTOM_HID_IDLE;
267  ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->Init();
268  /* Prepare Out endpoint to receive 1st packet */
270  USBD_CUSTOMHID_OUTREPORT_BUF_SIZE);
271  }
272 
273  return ret;
274 }
275 
283 static uint8_t USBD_CUSTOM_HID_DeInit (USBD_HandleTypeDef *pdev,
284  uint8_t cfgidx)
285 {
286  /* Close CUSTOM_HID EP IN */
287  USBD_LL_CloseEP(pdev,
289 
290  /* Close CUSTOM_HID EP OUT */
291  USBD_LL_CloseEP(pdev,
293 
294  /* FRee allocated memory */
295  if(pdev->pClassData != NULL)
296  {
297  ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->DeInit();
298  USBD_free(pdev->pClassData);
299  pdev->pClassData = NULL;
300  }
301  return USBD_OK;
302 }
303 
311 static uint8_t USBD_CUSTOM_HID_Setup (USBD_HandleTypeDef *pdev,
313 {
314  uint16_t len = 0;
315  uint8_t *pbuf = NULL;
317 
318  switch (req->bmRequest & USB_REQ_TYPE_MASK)
319  {
320  case USB_REQ_TYPE_CLASS :
321  switch (req->bRequest)
322  {
323 
324 
326  hhid->Protocol = (uint8_t)(req->wValue);
327  break;
328 
330  USBD_CtlSendData (pdev,
331  (uint8_t *)&hhid->Protocol,
332  1);
333  break;
334 
336  hhid->IdleState = (uint8_t)(req->wValue >> 8);
337  break;
338 
340  USBD_CtlSendData (pdev,
341  (uint8_t *)&hhid->IdleState,
342  1);
343  break;
344 
346  hhid->IsReportAvailable = 1;
347  USBD_CtlPrepareRx (pdev, hhid->Report_buf, (uint8_t)(req->wLength));
348 
349  break;
350  default:
351  USBD_CtlError (pdev, req);
352  return USBD_FAIL;
353  }
354  break;
355 
357  switch (req->bRequest)
358  {
360  if( req->wValue >> 8 == CUSTOM_HID_REPORT_DESC)
361  {
362  len = MIN(USBD_CUSTOM_HID_REPORT_DESC_SIZE , req->wLength);
363  pbuf = ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->pReport;
364  }
365  else if( req->wValue >> 8 == CUSTOM_HID_DESCRIPTOR_TYPE)
366  {
367  pbuf = USBD_CUSTOM_HID_Desc;
368  len = MIN(USB_CUSTOM_HID_DESC_SIZ , req->wLength);
369  }
370 
371  USBD_CtlSendData (pdev,
372  pbuf,
373  len);
374 
375  break;
376 
377  case USB_REQ_GET_INTERFACE :
378  USBD_CtlSendData (pdev,
379  (uint8_t *)&hhid->AltSetting,
380  1);
381  break;
382 
383  case USB_REQ_SET_INTERFACE :
384  hhid->AltSetting = (uint8_t)(req->wValue);
385  break;
386  }
387  }
388  return USBD_OK;
389 }
390 
399  uint8_t *report,
400  uint16_t len)
401 {
403 
405  {
406  if(hhid->state == CUSTOM_HID_IDLE)
407  {
408  hhid->state = CUSTOM_HID_BUSY;
409  USBD_LL_Transmit (pdev,
411  report,
412  len);
413  }
414  }
415  return USBD_OK;
416 }
417 
425 static uint8_t *USBD_CUSTOM_HID_GetCfgDesc (uint16_t *length)
426 {
427  *length = sizeof (USBD_CUSTOM_HID_CfgDesc);
428  return USBD_CUSTOM_HID_CfgDesc;
429 }
430 
438 static uint8_t USBD_CUSTOM_HID_DataIn (USBD_HandleTypeDef *pdev,
439  uint8_t epnum)
440 {
441 
442  /* Ensure that the FIFO is empty before a new transfer, this condition could
443  be caused by a new transfer before the end of the previous transfer */
445 
446  return USBD_OK;
447 }
448 
456 static uint8_t USBD_CUSTOM_HID_DataOut (USBD_HandleTypeDef *pdev,
457  uint8_t epnum)
458 {
459 
461 
462  ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->OutEvent(hhid->Report_buf[0],
463  hhid->Report_buf[1]);
464 
466  USBD_CUSTOMHID_OUTREPORT_BUF_SIZE);
467 
468  return USBD_OK;
469 }
470 
477 uint8_t USBD_CUSTOM_HID_EP0_RxReady(USBD_HandleTypeDef *pdev)
478 {
480 
481  if (hhid->IsReportAvailable == 1)
482  {
483  ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->OutEvent(hhid->Report_buf[0],
484  hhid->Report_buf[1]);
485  hhid->IsReportAvailable = 0;
486  }
487 
488  return USBD_OK;
489 }
490 
497 static uint8_t *USBD_CUSTOM_HID_GetDeviceQualifierDesc (uint16_t *length)
498 {
499  *length = sizeof (USBD_CUSTOM_HID_DeviceQualifierDesc);
500  return USBD_CUSTOM_HID_DeviceQualifierDesc;
501 }
502 
511 {
512  uint8_t ret = USBD_FAIL;
513 
514  if(fops != NULL)
515  {
516  pdev->pUserData= fops;
517  ret = USBD_OK;
518  }
519 
520  return ret;
521 }
536 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
Closes an endpoint of the Low Level Driver.
Definition: usbd_conf.c:492
uint8_t USBD_CUSTOM_HID_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_CUSTOM_HID_ItfTypeDef *fops)
USBD_CUSTOM_HID_RegisterInterface.
#define CUSTOM_HID_REQ_SET_PROTOCOL
uint8_t bmRequest
Definition: usbd_def.h:151
uint16_t wValue
Definition: usbd_def.h:153
#define CUSTOM_HID_EPOUT_SIZE
#define CUSTOM_HID_REQ_GET_IDLE
uint8_t bRequest
Definition: usbd_def.h:152
#define USB_LEN_DEV_QUALIFIER_DESC
Definition: usbd_def.h:57
#define CUSTOM_HID_EPIN_ADDR
USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t size)
Transmits data over an endpoint.
Definition: usbd_conf.c:574
uint8_t USBD_CUSTOM_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len)
USBD_CUSTOM_HID_SendReport Send CUSTOM_HID Report.
uint8_t Report_buf[USBD_CUSTOMHID_OUTREPORT_BUF_SIZE]
if(LCD_Lock==DISABLE)
Definition: lcd_log.c:249
#define USB_REQ_GET_DESCRIPTOR
Definition: usbd_def.h:87
#define USB_REQ_GET_INTERFACE
Definition: usbd_def.h:91
#define USBD_free
Definition: usbd_conf.h:74
USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t size)
Prepares an endpoint for reception.
Definition: usbd_conf.c:591
#define CUSTOM_HID_DESCRIPTOR_TYPE
#define NULL
Definition: usbd_def.h:53
#define CUSTOM_HID_REPORT_DESC
#define CUSTOM_HID_REQ_GET_PROTOCOL
#define __ALIGN_END
#define CUSTOM_HID_EPOUT_ADDR
#define USBD_malloc
Definition: usbd_conf.h:73
#define CUSTOM_HID_EPIN_SIZE
#define CUSTOM_HID_REQ_SET_REPORT
#define USB_REQ_SET_INTERFACE
Definition: usbd_def.h:92
uint16_t wLength
Definition: usbd_def.h:155
void USBD_CtlError(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
USBD_CtlError Handle USB low level Error.
Definition: usbd_ctlreq.c:716
#define USB_DESC_TYPE_ENDPOINT
Definition: usbd_def.h:99
Definition: pbuf.h:108
#define USB_DESC_TYPE_INTERFACE
Definition: usbd_def.h:98
#define USB_REQ_TYPE_CLASS
Definition: usbd_def.h:74
#define USB_DESC_TYPE_CONFIGURATION
Definition: usbd_def.h:96
#define USBD_EP_TYPE_INTR
Definition: usbd_def.h:136
#define USB_REQ_TYPE_STANDARD
Definition: usbd_def.h:73
USBD_StatusTypeDef USBD_CtlPrepareRx(USBD_HandleTypeDef *pdev, uint8_t *pbuf, uint16_t len)
USBD_CtlPrepareRx receive data on the ctl pipe.
Definition: usbd_ioreq.c:135
#define USB_CUSTOM_HID_CONFIG_DESC_SIZ
USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev, uint8_t *buf, uint16_t len)
USBD_CtlSendData send data on the ctl pipe.
Definition: usbd_ioreq.c:95
#define USB_DESC_TYPE_DEVICE_QUALIFIER
Definition: usbd_def.h:100
header file for the usbd_customhid.c file.
#define CUSTOM_HID_REQ_SET_IDLE
#define USB_REQ_TYPE_MASK
Definition: usbd_def.h:76
USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps)
Opens an endpoint of the Low Level Driver.
Definition: usbd_conf.c:473
#define USBD_STATE_CONFIGURED
Definition: usbd_def.h:120
CUSTOM_HID_StateTypeDef state
#define USB_CUSTOM_HID_DESC_SIZ
USBD_ClassTypeDef USBD_CUSTOM_HID
#define MIN(a, b)
Definition: usbd_def.h:265