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)
195 USB_EP_Init(pudev, CDC_DATA_IN_EP, USB_EPTYPE_BULK, CDC_DATA_IN_PACKET_SIZE);
196 USB_EP_Init(pudev, CDC_DATA_OUT_EP, USB_EPTYPE_BULK, CDC_DATA_OUT_PACKET_SIZE);
202 APP_FOPS.pIf_Init(DEFAULT_CONFIG);
205 USB_EP_Rx(pudev, CDC_DATA_OUT_EP, (uint8_t*)(USB_DATA_Buffer), CDC_DATA_OUT_PACKET_SIZE);
216 static uint8_t USBD_CDC_DeInit (
void *pudev, uint8_t ConfigIndex)
226 APP_FOPS.pIf_DeInit();
237 static uint8_t USBD_CDC_GetClassDescriptor (
void *pudev,
USB_DEVICE_REQ *req)
239 uint16_t len = USB_CDC_DESC_SIZE;
240 uint8_t *pbuf= (uint8_t*)USBD_CDC_CfgDesc + 9;
242 if((req->wValue >> 8) == CDC_DESC_TYPE)
244 len = MIN(USB_CDC_DESC_SIZE, req->wLength);
245 pbuf = (uint8_t*)USBD_CDC_CfgDesc + 9 + (9 * USBD_ITF_MAX_NUM);
259 static uint8_t USBD_CDC_ClassReqHandle (
void *pudev,
USB_DEVICE_REQ *req)
263 if (req->bmRequestType & 0x80)
268 APP_FOPS.pIf_Ctrl(req->bRequest, USB_CMD_Buffer, req->wLength);
271 USB_CtlTx (pudev, USB_CMD_Buffer, req->wLength);
278 CDCCmd = req->bRequest;
279 CDCLen = req->wLength;
282 USB_CtlRx (pudev, USB_CMD_Buffer, req->wLength);
288 APP_FOPS.pIf_Ctrl(req->bRequest, NULL, 0);
300 static uint8_t USBD_CDC_GetInterface (
void *pudev,
USB_DEVICE_REQ *req)
302 USB_CtlTx (pudev, (uint8_t *)&USBD_CDC_AltSet, 1);
313 static uint8_t USBD_CDC_SetInterface (
void *pudev,
USB_DEVICE_REQ *req)
315 if ((uint8_t)(req->wValue) < USBD_ITF_MAX_NUM)
317 USBD_CDC_AltSet = (uint8_t)(req->wValue);
333 static uint8_t USBD_CDC_EP0_RxReady (
void *pudev)
335 if (CDCCmd != NO_CMD)
338 APP_FOPS.pIf_Ctrl(CDCCmd, USB_CMD_Buffer, CDCLen);
353 static uint8_t USBD_CDC_DataIn (
void *pudev, uint8_t EpID)
355 uint16_t USB_Tx_length;
357 if (USB_Tx_State == 1)
359 if (AppRxLength == 0)
375 if (AppRxLength > CDC_DATA_IN_PACKET_SIZE)
377 USB_Tx_length = CDC_DATA_IN_PACKET_SIZE;
381 USB_Tx_length = AppRxLength;
382 if (AppRxLength == CDC_DATA_IN_PACKET_SIZE) end_packet = 1;
388 &APP_DATA_Buffer[AppBufOutPtr],
391 AppBufOutPtr += USB_Tx_length;
392 AppRxLength -= USB_Tx_length;
405 static uint8_t USBD_CDC_DataOut (
void *pudev, uint8_t EpID)
413 APP_FOPS.pIf_DataRx(COM2, USB_DATA_Buffer, USB_Rx_Cnt);
419 CDC_DATA_OUT_PACKET_SIZE);
429 static uint8_t USBD_CDC_SOF (
void *pudev)
431 static uint8_t FrameCount = 0;
439 USBD_CDC_AsynchXferHandle(pudev);
450 static void USBD_CDC_AsynchXferHandle (
void *pudev)
452 uint16_t USB_Tx_length;
454 if(USB_Tx_State != 1)
456 if(AppBufOutPtr == AppBufInPtr)
462 if(AppBufOutPtr > AppBufInPtr)
468 AppRxLength = AppBufInPtr - AppBufOutPtr;
471 if (AppRxLength > CDC_DATA_IN_PACKET_SIZE)
473 USB_Tx_length = CDC_DATA_IN_PACKET_SIZE;
477 USB_Tx_length = AppRxLength;
478 if (USB_Tx_length == CDC_DATA_IN_PACKET_SIZE) end_packet = 1;
483 &APP_DATA_Buffer[AppBufOutPtr],
487 AppBufOutPtr += USB_Tx_length;
488 AppRxLength -= USB_Tx_length;
505 static uint8_t *USBD_CDC_GetCfgDesc (uint8_t USBSpeed, uint16_t *len)
507 *len =
sizeof (USBD_CDC_CfgDesc);
509 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.