30 static uint8_t USBD_CDC_Init (
void *pudev, uint8_t ConfigIndex);
31 static uint8_t USBD_CDC_DeInit (
void *pudev, uint8_t ConfigIndex);
32 static uint8_t USBD_CDC_GetClassDescriptor (
void *pudev,
USB_DEVICE_REQ *req);
33 static uint8_t USBD_CDC_ClassReqHandle (
void *pudev,
USB_DEVICE_REQ *req);
34 static uint8_t USBD_CDC_GetInterface (
void *pudev,
USB_DEVICE_REQ *req);
35 static uint8_t USBD_CDC_SetInterface (
void *pudev,
USB_DEVICE_REQ *req);
36 static uint8_t USBD_CDC_EP0_RxReady (
void *pudev);
37 static uint8_t USBD_CDC_DataIn (
void *pudev, uint8_t EpID);
38 static uint8_t USBD_CDC_DataOut (
void *pudev, uint8_t EpID);
39 static uint8_t USBD_CDC_SOF (
void *pudev);
40 static uint8_t* USBD_CDC_GetCfgDesc (uint8_t USBSpeed, uint16_t *len);
42 static void USBD_CDC_AsynchXferHandle (
void *pudev);
49 uint8_t USB_Tx_State = 0;
51 static __IO uint32_t USBD_CDC_AltSet = 0;
53 uint8_t USB_DATA_Buffer[USB_CDC_DATA_PACKET_SIZE];
54 uint8_t USB_CMD_Buffer[USB_CDC_CMD_PACKET_SIZE];
57 __IO uint32_t end_packet = 0;
59 uint32_t AppBufInPtr = 0;
60 uint32_t AppBufOutPtr = 0;
61 uint32_t AppRxLength = 0;
63 static uint32_t CDCCmd = 0xFF;
64 static uint32_t CDCLen = 0;
71 USBD_CDC_GetClassDescriptor,
72 USBD_CDC_ClassReqHandle,
73 USBD_CDC_GetInterface,
74 USBD_CDC_SetInterface,
84 const uint8_t USBD_CDC_CfgDesc[USB_CDC_CONFIG_DESC_SIZE] =
88 USB_DESCTYPE_CONFIGURATION,
89 USB_CDC_CONFIG_DESC_SIZE,
99 USB_DESCTYPE_INTERFACE,
137 USB_DESCTYPE_ENDPOINT,
140 LOWBYTE(USB_CDC_CMD_PACKET_SIZE),
141 HIGHBYTE(USB_CDC_CMD_PACKET_SIZE),
146 USB_DESCTYPE_INTERFACE,
157 USB_DESCTYPE_ENDPOINT,
160 LOWBYTE(CDC_DATA_OUT_PACKET_SIZE),
161 HIGHBYTE(CDC_DATA_OUT_PACKET_SIZE),
166 USB_DESCTYPE_ENDPOINT,
169 LOWBYTE(CDC_DATA_IN_PACKET_SIZE),
170 HIGHBYTE(CDC_DATA_IN_PACKET_SIZE),
188 static uint8_t USBD_CDC_Init (
void *pudev, uint8_t ConfigIndex)
190 #ifdef DOUBLE_BUFFER_TX
198 #ifdef DOUBLE_BUFFER_RX
205 USB_EP_Init(pudev, CDC_DATA_IN_EP, USB_EPTYPE_BULK, CDC_DATA_IN_PACKET_SIZE);
206 USB_EP_Init(pudev, CDC_DATA_OUT_EP, USB_EPTYPE_BULK, CDC_DATA_OUT_PACKET_SIZE);
212 APP_FOPS.pIf_Init(DEFAULT_CONFIG);
215 USB_EP_Rx(pudev, CDC_DATA_OUT_EP, (uint8_t*)(USB_DATA_Buffer), CDC_DATA_OUT_PACKET_SIZE);
226 static uint8_t USBD_CDC_DeInit (
void *pudev, uint8_t ConfigIndex)
236 APP_FOPS.pIf_DeInit();
247 static uint8_t USBD_CDC_GetClassDescriptor (
void *pudev,
USB_DEVICE_REQ *req)
249 uint16_t len = USB_CDC_DESC_SIZE;
250 uint8_t *pbuf= (uint8_t*)USBD_CDC_CfgDesc + 9;
252 if((req->wValue >> 8) == CDC_DESC_TYPE)
254 len = MIN(USB_CDC_DESC_SIZE, req->wLength);
255 pbuf = (uint8_t*)USBD_CDC_CfgDesc + 9 + (9 * USBD_ITF_MAX_NUM);
269 static uint8_t USBD_CDC_ClassReqHandle (
void *pudev,
USB_DEVICE_REQ *req)
273 if (req->bmRequestType & 0x80)
278 APP_FOPS.pIf_Ctrl(req->bRequest, USB_CMD_Buffer, req->wLength);
281 USB_CtlTx (pudev, USB_CMD_Buffer, req->wLength);
288 CDCCmd = req->bRequest;
289 CDCLen = req->wLength;
292 USB_CtlRx (pudev, USB_CMD_Buffer, req->wLength);
298 APP_FOPS.pIf_Ctrl(req->bRequest, NULL, 0);
310 static uint8_t USBD_CDC_GetInterface (
void *pudev,
USB_DEVICE_REQ *req)
312 USB_CtlTx (pudev, (uint8_t *)&USBD_CDC_AltSet, 1);
323 static uint8_t USBD_CDC_SetInterface (
void *pudev,
USB_DEVICE_REQ *req)
325 if ((uint8_t)(req->wValue) < USBD_ITF_MAX_NUM)
327 USBD_CDC_AltSet = (uint8_t)(req->wValue);
343 static uint8_t USBD_CDC_EP0_RxReady (
void *pudev)
345 if (CDCCmd != NO_CMD)
348 APP_FOPS.pIf_Ctrl(CDCCmd, USB_CMD_Buffer, CDCLen);
363 static uint8_t USBD_CDC_DataIn (
void *pudev, uint8_t EpID)
365 uint16_t USB_Tx_length;
367 if (USB_Tx_State == 1)
369 if (AppRxLength == 0)
385 if (AppRxLength > CDC_DATA_IN_PACKET_SIZE)
387 USB_Tx_length = CDC_DATA_IN_PACKET_SIZE;
391 USB_Tx_length = AppRxLength;
392 if (AppRxLength == CDC_DATA_IN_PACKET_SIZE) end_packet = 1;
398 &APP_DATA_Buffer[AppBufOutPtr],
401 AppBufOutPtr += USB_Tx_length;
402 AppRxLength -= USB_Tx_length;
415 static uint8_t USBD_CDC_DataOut (
void *pudev, uint8_t EpID)
423 APP_FOPS.pIf_DataRx(USB_DATA_Buffer, USB_Rx_Cnt);
429 CDC_DATA_OUT_PACKET_SIZE);
439 static uint8_t USBD_CDC_SOF (
void *pudev)
441 static uint8_t FrameCount = 0;
449 USBD_CDC_AsynchXferHandle(pudev);
460 static void USBD_CDC_AsynchXferHandle (
void *pudev)
462 uint16_t USB_Tx_length;
464 if(USB_Tx_State != 1)
466 if(AppBufOutPtr == AppBufInPtr)
472 if(AppBufOutPtr > AppBufInPtr)
478 AppRxLength = AppBufInPtr - AppBufOutPtr;
481 if (AppRxLength > CDC_DATA_IN_PACKET_SIZE)
483 USB_Tx_length = CDC_DATA_IN_PACKET_SIZE;
487 USB_Tx_length = AppRxLength;
488 if (USB_Tx_length == CDC_DATA_IN_PACKET_SIZE) end_packet = 1;
493 &APP_DATA_Buffer[AppBufOutPtr],
497 AppBufOutPtr += USB_Tx_length;
498 AppRxLength -= USB_Tx_length;
515 static uint8_t *USBD_CDC_GetCfgDesc (uint8_t USBSpeed, uint16_t *len)
517 *len =
sizeof (USBD_CDC_CfgDesc);
519 return (uint8_t*)USBD_CDC_CfgDesc;
USB device class callback type define.
void USB_EP_Tx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to transmit data.
Header file for the usbd_cdc_core.c file.
void USB_EP_DeInit(USB_CORE_HANDLE *pudev, uint8_t EpAddr)
Configure the endpoint when it is disabled.
void USB_EP_Init(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t EpType, uint16_t EpMps)
Endpoint initialization.
uint8_t USB_CtlRx(USB_CORE_HANDLE *pudev, uint8_t *pbuf, uint16_t Len)
Receive data on the control pipe.
uint8_t USB_CtlTx(USB_CORE_HANDLE *pudev, uint8_t *pbuf, uint16_t Len)
Transmit data on the control pipe.
USB enumeration function prototypes.
#define USB_SNG_BUFTYPE
USB endpoint kind.
void USB_EP_BufConfig(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t EpKind, uint32_t BufAddr)
Configure buffer for endpoint.
void USBD_EnumError(USB_DEVICE_HANDLE *pudev, USB_DEVICE_REQ *req)
Handle usb enumeration error event.
USB standard device request struct.
void USB_EP_Rx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to receive data.