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_GetCfgDesc (uint8_t USBSpeed, uint16_t *len);
44 static __IO uint32_t USBD_CDC_AltSet = 0;
46 uint8_t USB_DATA_Buffer[USB_CDC_DATA_PACKET_SIZE];
47 uint8_t USB_CMD_Buffer[USB_CDC_CMD_PACKET_SIZE];
49 extern uint8_t PacketSent;
50 extern uint8_t PacketReceive;
52 static uint32_t CDCCmd = 0xFF;
54 uint32_t ReceiveLength = 0;
70 USBD_CDC_GetClassDescriptor,
71 USBD_CDC_ClassReqHandle,
72 USBD_CDC_GetInterface,
73 USBD_CDC_SetInterface,
83 const uint8_t USBD_CDC_CfgDesc[USB_CDC_CONFIG_DESC_SIZE] =
87 USB_DESCTYPE_CONFIGURATION,
88 USB_CDC_CONFIG_DESC_SIZE,
98 USB_DESCTYPE_INTERFACE,
136 USB_DESCTYPE_ENDPOINT,
139 LOWBYTE(USB_CDC_CMD_PACKET_SIZE),
140 HIGHBYTE(USB_CDC_CMD_PACKET_SIZE),
145 USB_DESCTYPE_INTERFACE,
156 USB_DESCTYPE_ENDPOINT,
159 LOWBYTE(CDC_DATA_OUT_PACKET_SIZE),
160 HIGHBYTE(CDC_DATA_OUT_PACKET_SIZE),
165 USB_DESCTYPE_ENDPOINT,
168 LOWBYTE(CDC_DATA_IN_PACKET_SIZE),
169 HIGHBYTE(CDC_DATA_IN_PACKET_SIZE),
187 static uint8_t USBD_CDC_Init (
void *pudev, uint8_t ConfigIndex)
194 USB_EP_Init(pudev, CDC_DATA_IN_EP, USB_EPTYPE_BULK, CDC_DATA_IN_PACKET_SIZE);
195 USB_EP_Init(pudev, CDC_DATA_OUT_EP, USB_EPTYPE_BULK, CDC_DATA_OUT_PACKET_SIZE);
209 static uint8_t USBD_CDC_DeInit (
void *pudev, uint8_t ConfigIndex)
227 static uint8_t USBD_CDC_GetClassDescriptor (
void *pudev,
USB_DEVICE_REQ *req)
229 uint16_t len = USB_CDC_DESC_SIZE;
230 uint8_t *pbuf= (uint8_t*)USBD_CDC_CfgDesc + 9;
232 if((req->wValue >> 8) == CDC_DESC_TYPE)
234 len = MIN(USB_CDC_DESC_SIZE, req->wLength);
235 pbuf = (uint8_t*)USBD_CDC_CfgDesc + 9 + (9 * USBD_ITF_MAX_NUM);
249 static uint8_t USBD_CDC_ClassReqHandle (
void *pudev,
USB_DEVICE_REQ *req)
251 switch (req->bRequest)
253 case SEND_ENCAPSULATED_COMMAND:
257 case GET_ENCAPSULATED_RESPONSE:
261 case SET_COMM_FEATURE:
265 case GET_COMM_FEATURE:
269 case CLEAR_COMM_FEATURE:
273 case SET_LINE_CODING:
275 CDCCmd = req->bRequest;
278 USB_CtlRx (pudev, USB_CMD_Buffer, req->wLength);
281 case GET_LINE_CODING:
282 USB_CMD_Buffer[0] = (uint8_t)(linecoding.dwDTERate);
283 USB_CMD_Buffer[1] = (uint8_t)(linecoding.dwDTERate >> 8);
284 USB_CMD_Buffer[2] = (uint8_t)(linecoding.dwDTERate >> 16);
285 USB_CMD_Buffer[3] = (uint8_t)(linecoding.dwDTERate >> 24);
286 USB_CMD_Buffer[4] = linecoding.bCharFormat;
287 USB_CMD_Buffer[5] = linecoding.bParityType;
288 USB_CMD_Buffer[6] = linecoding.bDataBits;
291 USB_CtlTx (pudev, USB_CMD_Buffer, req->wLength);
294 case SET_CONTROL_LINE_STATE:
315 static uint8_t USBD_CDC_GetInterface (
void *pudev,
USB_DEVICE_REQ *req)
317 USB_CtlTx (pudev, (uint8_t *)&USBD_CDC_AltSet, 1);
328 static uint8_t USBD_CDC_SetInterface (
void *pudev,
USB_DEVICE_REQ *req)
330 if ((uint8_t)(req->wValue) < USBD_ITF_MAX_NUM)
332 USBD_CDC_AltSet = (uint8_t)(req->wValue);
348 static uint8_t USBD_CDC_EP0_RxReady (
void *pudev)
350 if (CDCCmd != NO_CMD)
353 linecoding.dwDTERate = (uint32_t)(USB_CMD_Buffer[0] |
354 (USB_CMD_Buffer[1] << 8) |
355 (USB_CMD_Buffer[2] << 16) |
356 (USB_CMD_Buffer[3] << 24));
358 linecoding.bCharFormat = USB_CMD_Buffer[4];
359 linecoding.bParityType = USB_CMD_Buffer[5];
360 linecoding.bDataBits = USB_CMD_Buffer[6];
374 static uint8_t USBD_CDC_DataIn (
void *pudev, uint8_t EpID)
387 static uint8_t USBD_CDC_DataOut (
void *pudev, uint8_t EpID)
391 ReceiveLength = _GetEPRxCount(EpID);
402 static uint8_t *USBD_CDC_GetCfgDesc (uint8_t USBSpeed, uint16_t *len)
404 *len =
sizeof (USBD_CDC_CfgDesc);
406 return (uint8_t*)USBD_CDC_CfgDesc;
417 if(DataLen <= USB_CDC_DATA_PACKET_SIZE)
421 USB_EP_Tx(pudev, CDC_DATA_IN_EP, (uint8_t*)(USB_DATA_Buffer), DataLen);
434 USB_EP_Rx(pudev, CDC_DATA_OUT_EP, (uint8_t*)(USB_DATA_Buffer), CDC_DATA_OUT_PACKET_SIZE);
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.
void USBD_CDC_SendData(void *pudev, uint32_t DataLen)
Send the data received from the GD32 to the PC through USB.
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.
Header file for the usbd_cdc_core.c file.
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.
void USBD_CDC_ReceiveData(void *pudev)
Receive the data from the PC to GD32 and send it through USB.
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.