usbd_core.c
Go to the documentation of this file.
1 
28 /* Includes ------------------------------------------------------------------*/
29 #include "usbd_core.h"
30 #include "usbd_req.h"
31 #include "usbd_ioreq.h"
32 #include "usb_dcd_int.h"
33 #include "usb_bsp.h"
34 
75 static uint8_t USBD_SetupStage(USB_OTG_CORE_HANDLE *pdev);
76 static uint8_t USBD_DataOutStage(USB_OTG_CORE_HANDLE *pdev , uint8_t epnum);
77 static uint8_t USBD_DataInStage(USB_OTG_CORE_HANDLE *pdev , uint8_t epnum);
78 static uint8_t USBD_SOF(USB_OTG_CORE_HANDLE *pdev);
79 static uint8_t USBD_Reset(USB_OTG_CORE_HANDLE *pdev);
80 static uint8_t USBD_Suspend(USB_OTG_CORE_HANDLE *pdev);
81 static uint8_t USBD_Resume(USB_OTG_CORE_HANDLE *pdev);
82 #ifdef VBUS_SENSING_ENABLED
83 static uint8_t USBD_DevConnected(USB_OTG_CORE_HANDLE *pdev);
84 static uint8_t USBD_DevDisconnected(USB_OTG_CORE_HANDLE *pdev);
85 #endif
86 static uint8_t USBD_IsoINIncomplete(USB_OTG_CORE_HANDLE *pdev);
87 static uint8_t USBD_IsoOUTIncomplete(USB_OTG_CORE_HANDLE *pdev);
88 static uint8_t USBD_RunTestMode (USB_OTG_CORE_HANDLE *pdev) ;
98 
100 {
104  USBD_SOF,
105  USBD_Reset,
106  USBD_Suspend,
107  USBD_Resume,
110 #ifdef VBUS_SENSING_ENABLED
111  USBD_DevConnected,
112  USBD_DevDisconnected,
113 #else
114  NULL,
115  NULL,
116 #endif
117 };
118 
139  USBD_DEVICE *pDevice,
140  USBD_Class_cb_TypeDef *class_cb,
141  USBD_Usr_cb_TypeDef *usr_cb)
142 {
143  /* Hardware Init */
144  USB_OTG_BSP_Init(pdev);
145 
146  USBD_DeInit(pdev);
147 
148  /*Register class and user callbacks */
149  pdev->dev.class_cb = class_cb;
150  pdev->dev.usr_cb = usr_cb;
151  pdev->dev.usr_device = pDevice;
152 
153  /* set USB OTG core params */
154  DCD_Init(pdev , coreID);
155 
156  /* Upon Init call usr callback */
157  pdev->dev.usr_cb->Init();
158 
159  /* Enable Interrupts */
161 }
162 
170 {
171  /* Software Init */
172  (void)pdev;
173 
174  return USBD_OK;
175 }
176 
184 {
185  USB_SETUP_REQ req;
186 
187  USBD_ParseSetupRequest(pdev , &req);
188 
189  switch (req.bmRequest & 0x1F)
190  {
192  USBD_StdDevReq (pdev, &req);
193  break;
194 
196  USBD_StdItfReq(pdev, &req);
197  break;
198 
200  USBD_StdEPReq(pdev, &req);
201  break;
202 
203  default:
204  DCD_EP_Stall(pdev , req.bmRequest & 0x80);
205  break;
206  }
207  return USBD_OK;
208 }
209 
217 static uint8_t USBD_DataOutStage(USB_OTG_CORE_HANDLE *pdev , uint8_t epnum)
218 {
219  USB_OTG_EP *ep;
220 
221  if(epnum == 0)
222  {
223  ep = &pdev->dev.out_ep[0];
224  if ( pdev->dev.device_state == USB_OTG_EP0_DATA_OUT)
225  {
226  if(ep->rem_data_len > ep->maxpacket)
227  {
228  ep->rem_data_len -= ep->maxpacket;
229 
230  if(pdev->cfg.dma_enable == 1)
231  {
232  /* in slave mode this, is handled by the RxSTSQLvl ISR */
233  ep->xfer_buff += ep->maxpacket;
234  }
235  USBD_CtlContinueRx (pdev,
236  ep->xfer_buff,
237  MIN(ep->rem_data_len ,ep->maxpacket));
238  }
239  else
240  {
241  if((pdev->dev.class_cb->EP0_RxReady != NULL)&&
242  (pdev->dev.device_status == USB_OTG_CONFIGURED))
243  {
244  pdev->dev.class_cb->EP0_RxReady(pdev);
245  }
246  USBD_CtlSendStatus(pdev);
247  }
248  }
249  }
250  else if((pdev->dev.class_cb->DataOut != NULL)&&
251  (pdev->dev.device_status == USB_OTG_CONFIGURED))
252  {
253  pdev->dev.class_cb->DataOut(pdev, epnum);
254  }
255 
256  else
257  {
258  /* Do Nothing */
259  }
260  return USBD_OK;
261 }
262 
270 static uint8_t USBD_DataInStage(USB_OTG_CORE_HANDLE *pdev , uint8_t epnum)
271 {
272  USB_OTG_EP *ep;
273 
274  if(epnum == 0)
275  {
276  ep = &pdev->dev.in_ep[0];
277  if ( pdev->dev.device_state == USB_OTG_EP0_DATA_IN)
278  {
279  if(ep->rem_data_len > ep->maxpacket)
280  {
281  ep->rem_data_len -= ep->maxpacket;
282  if(pdev->cfg.dma_enable == 1)
283  {
284  /* in slave mode this, is handled by the TxFifoEmpty ISR */
285  ep->xfer_buff += ep->maxpacket;
286  }
288  ep->xfer_buff,
289  ep->rem_data_len);
290 
291  /* Start the transfer */
292  DCD_EP_PrepareRx (pdev,
293  0,
294  NULL,
295  0);
296  }
297  else
298  { /* last packet is MPS multiple, so send ZLP packet */
299  if((ep->total_data_len % ep->maxpacket == 0) &&
300  (ep->total_data_len >= ep->maxpacket) &&
301  (ep->total_data_len < ep->ctl_data_len ))
302  {
303 
304  USBD_CtlContinueSendData(pdev , NULL, 0);
305  ep->ctl_data_len = 0;
306 
307  /* Start the transfer */
308  DCD_EP_PrepareRx (pdev,
309  0,
310  NULL,
311  0);
312  }
313  else
314  {
315  if((pdev->dev.class_cb->EP0_TxSent != NULL)&&
316  (pdev->dev.device_status == USB_OTG_CONFIGURED))
317  {
318  pdev->dev.class_cb->EP0_TxSent(pdev);
319  }
320  USBD_CtlReceiveStatus(pdev);
321  }
322  }
323  }
324  if (pdev->dev.test_mode == 1)
325  {
326  USBD_RunTestMode(pdev);
327  pdev->dev.test_mode = 0;
328  }
329  }
330  else if((pdev->dev.class_cb->DataIn != NULL)&&
331  (pdev->dev.device_status == USB_OTG_CONFIGURED))
332  {
333  pdev->dev.class_cb->DataIn(pdev, epnum);
334  }
335 
336  else
337  {
338  /* Do Nothing */
339  }
340  return USBD_OK;
341 }
342 
343 
344 
345 
352 static uint8_t USBD_RunTestMode (USB_OTG_CORE_HANDLE *pdev)
353 {
355  return USBD_OK;
356 }
357 
365 static uint8_t USBD_Reset(USB_OTG_CORE_HANDLE *pdev)
366 {
367  /* Open EP0 OUT */
368  DCD_EP_Open(pdev,
369  0x00,
371  EP_TYPE_CTRL);
372 
373  /* Open EP0 IN */
374  DCD_EP_Open(pdev,
375  0x80,
377  EP_TYPE_CTRL);
378 
379  /* Upon Reset call usr call back */
380  pdev->dev.device_status = USB_OTG_DEFAULT;
381  pdev->dev.usr_cb->DeviceReset(pdev->cfg.speed);
382 
383  return USBD_OK;
384 }
385 
393 static uint8_t USBD_Resume(USB_OTG_CORE_HANDLE *pdev)
394 {
395  /* Upon Resume call usr call back */
396  pdev->dev.usr_cb->DeviceResumed();
397  pdev->dev.device_status = pdev->dev.device_old_status;
398  pdev->dev.device_status = USB_OTG_CONFIGURED;
399  return USBD_OK;
400 }
401 
402 
410 static uint8_t USBD_Suspend(USB_OTG_CORE_HANDLE *pdev)
411 {
412  pdev->dev.device_old_status = pdev->dev.device_status;
413  pdev->dev.device_status = USB_OTG_SUSPENDED;
414  /* Upon Resume call usr call back */
415  pdev->dev.usr_cb->DeviceSuspended();
416  return USBD_OK;
417 }
418 
419 
427 static uint8_t USBD_SOF(USB_OTG_CORE_HANDLE *pdev)
428 {
429  if(pdev->dev.class_cb->SOF)
430  {
431  pdev->dev.class_cb->SOF(pdev);
432  }
433  return USBD_OK;
434 }
444 {
445  pdev->dev.class_cb->Init(pdev, cfgidx);
446 
447  /* Upon set config call usr call back */
448  pdev->dev.usr_cb->DeviceConfigured();
449  return USBD_OK;
450 }
451 
460 {
461  pdev->dev.class_cb->DeInit(pdev, cfgidx);
462  return USBD_OK;
463 }
464 
472 {
473  pdev->dev.class_cb->IsoINIncomplete(pdev);
474  return USBD_OK;
475 }
476 
484 {
485  pdev->dev.class_cb->IsoOUTIncomplete(pdev);
486  return USBD_OK;
487 }
488 
489 #ifdef VBUS_SENSING_ENABLED
490 
496 static uint8_t USBD_DevConnected(USB_OTG_CORE_HANDLE *pdev)
497 {
498  pdev->dev.usr_cb->DeviceConnected();
499  pdev->dev.connection_status = 1;
500  return USBD_OK;
501 }
502 
509 static uint8_t USBD_DevDisconnected(USB_OTG_CORE_HANDLE *pdev)
510 {
511  pdev->dev.usr_cb->DeviceDisconnected();
512  pdev->dev.class_cb->DeInit(pdev, 0);
513  pdev->dev.connection_status = 0;
514  return USBD_OK;
515 }
516 #endif
517 
531 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
532 
USBD_Status USBD_StdItfReq(USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req)
USBD_StdItfReq Handle standard usb interface requests.
Definition: usbd_req.c:194
static uint8_t USBD_IsoOUTIncomplete(USB_OTG_CORE_HANDLE *pdev)
USBD_IsoOUTIncomplete Handle iso out incomplete event.
Definition: usbd_core.c:483
#define USB_OTG_DEFAULT
Definition: usb_dcd.h:56
Specific api&#39;s relative to the used hardware platform.
header file for the usbd_req.c file
USBD_Status
Definition: usbd_core.h:51
USBD_Status USBD_CtlContinueRx(USB_OTG_CORE_HANDLE *pdev, uint8_t *pbuf, uint16_t len)
USBD_CtlContinueRx continue receive data on the ctl pipe.
Definition: usbd_ioreq.c:165
USBD_Status USBD_CtlContinueSendData(USB_OTG_CORE_HANDLE *pdev, uint8_t *pbuf, uint16_t len)
USBD_CtlContinueSendData continue sending data on the ctl pipe.
Definition: usbd_ioreq.c:118
static uint8_t USBD_IsoINIncomplete(USB_OTG_CORE_HANDLE *pdev)
USBD_IsoINIncomplete Handle iso in incomplete event.
Definition: usbd_core.c:471
uint32_t ctl_data_len
Definition: usb_core.h:154
static uint8_t USBD_DataOutStage(USB_OTG_CORE_HANDLE *pdev, uint8_t epnum)
USBD_DataOutStage Handle data out stage.
Definition: usbd_core.c:217
static uint8_t USBD_RunTestMode(USB_OTG_CORE_HANDLE *pdev)
USBD_RunTestMode Launch test mode process.
Definition: usbd_core.c:352
uint32_t DCD_EP_Stall(USB_OTG_CORE_HANDLE *pdev, uint8_t epnum)
Stall an endpoint.
Definition: usb_dcd.c:306
USBD_Status USBD_ClrCfg(USB_OTG_CORE_HANDLE *pdev, uint8_t cfgidx)
USBD_ClrCfg Clear current configuration.
Definition: usbd_core.c:459
#define EP_TYPE_CTRL
Definition: usb_defines.h:140
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
USB_OTG_BSP_Init Initilizes BSP configurations.
Definition: usb_bsp.c:46
USB_OTG_CORE_ID_TypeDef
Definition: usb_defines.h:190
#define USB_OTG_SUSPENDED
Definition: usb_dcd.h:59
#define USB_OTG_MAX_EP0_SIZE
Definition: usb_regs.h:71
USB_OTG_CORE_CFGS cfg
Definition: usb_core.h:289
#define MIN(a, b)
Definition: usb_defines.h:178
Peripheral Device Interface Layer.
uint8_t speed
Definition: usb_core.h:166
#define USB_OTG_EP0_DATA_OUT
Definition: usb_core.h:55
__IO USB_OTG_DCTL_TypeDef SET_TEST_MODE
Definition: usbd_core.c:97
static uint8_t USBD_SOF(USB_OTG_CORE_HANDLE *pdev)
USBD_SOF Handle SOF event.
Definition: usbd_core.c:427
#define __IO
Definition: core_cm0.h:198
#define USB_OTG_WRITE_REG32(reg, value)
Definition: usb_defines.h:226
uint8_t * xfer_buff
Definition: usb_core.h:147
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 USB_REQ_RECIPIENT_DEVICE
Definition: usbd_def.h:72
USBD_Status USBD_CtlSendStatus(USB_OTG_CORE_HANDLE *pdev)
USBD_CtlSendStatus send zero length packet on the ctl pipe.
Definition: usbd_ioreq.c:183
USBD_Status USBD_DeInit(USB_OTG_CORE_HANDLE *pdev)
USBD_DeInit Re-Initialize the device library.
Definition: usbd_core.c:169
static uint8_t USBD_Reset(USB_OTG_CORE_HANDLE *pdev)
USBD_Reset Handle Reset event.
Definition: usbd_core.c:365
__IO uint32_t DCTL
Definition: usb_regs.h:116
uint8_t bmRequest
Definition: usb_core.h:182
static uint8_t USBD_Suspend(USB_OTG_CORE_HANDLE *pdev)
USBD_Suspend Handle Suspend event.
Definition: usbd_core.c:410
static uint8_t USBD_DataInStage(USB_OTG_CORE_HANDLE *pdev, uint8_t epnum)
USBD_DataInStage Handle data in stage.
Definition: usbd_core.c:270
uint32_t DCD_EP_PrepareRx(USB_OTG_CORE_HANDLE *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t buf_len)
DCD_EP_PrepareRx.
Definition: usb_dcd.c:231
uint8_t dma_enable
Definition: usb_core.h:167
header file for the usbd_ioreq.c file
uint32_t maxpacket
Definition: usb_core.h:145
USBD_DCD_INT_cb_TypeDef USBD_DCD_INT_cb
Definition: usbd_core.c:99
void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev)
USB_OTG_BSP_EnableInterrupt Enabele USB Global interrupt.
Definition: usb_bsp.c:96
uint32_t rem_data_len
Definition: usb_core.h:152
static uint8_t USBD_SetupStage(USB_OTG_CORE_HANDLE *pdev)
USBD_SetupStage Handle the setup stage.
Definition: usbd_core.c:183
USBD_Status USBD_CtlReceiveStatus(USB_OTG_CORE_HANDLE *pdev)
USBD_CtlReceiveStatus receive zero length packet on the ctl pipe.
Definition: usbd_ioreq.c:203
#define USB_OTG_EP0_DATA_IN
Definition: usb_core.h:54
#define USB_REQ_RECIPIENT_INTERFACE
Definition: usbd_def.h:73
USBD_Status USBD_StdEPReq(USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req)
USBD_StdEPReq Handle standard usb endpoint requests.
Definition: usbd_req.c:231
void USBD_Init(USB_OTG_CORE_HANDLE *pdev, USB_OTG_CORE_ID_TypeDef coreID, USBD_DEVICE *pDevice, USBD_Class_cb_TypeDef *class_cb, USBD_Usr_cb_TypeDef *usr_cb)
USBD_Init Initializes the device stack and load the class driver.
Definition: usbd_core.c:137
#define NULL
Definition: usbd_def.h:50
uint32_t total_data_len
Definition: usb_core.h:153
void DCD_Init(USB_OTG_CORE_HANDLE *pdev, USB_OTG_CORE_ID_TypeDef coreID)
Definition: usb_dcd.c:91
#define USB_OTG_CONFIGURED
Definition: usb_dcd.h:58
USBD_Status USBD_SetCfg(USB_OTG_CORE_HANDLE *pdev, uint8_t cfgidx)
USBD_SetCfg Configure device and start the interface.
Definition: usbd_core.c:443
#define USB_REQ_RECIPIENT_ENDPOINT
Definition: usbd_def.h:74
USB_OTG_CORE_REGS regs
Definition: usb_core.h:290
Header file for usbd_core.c.
USBD_DCD_INT_cb_TypeDef * USBD_DCD_INT_fops
Definition: usbd_core.c:119
USBD_Status USBD_StdDevReq(USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req)
USBD_StdDevReq Handle standard usb device requests.
Definition: usbd_req.c:143
USB_OTG_DREGS * DREGS
Definition: usb_regs.h:225
void USBD_ParseSetupRequest(USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req)
USBD_ParseSetupRequest Copy buffer into setup structure.
Definition: usbd_req.c:792
static uint8_t USBD_Resume(USB_OTG_CORE_HANDLE *pdev)
USBD_Resume Handle Resume event.
Definition: usbd_core.c:393


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:26