30 static uint8_t USBD_CUSTOMHID_Init (
void *pudev, uint8_t ConfigIndex);
31 static uint8_t USBD_CUSTOMHID_DeInit (
void *pudev, uint8_t ConfigIndex);
32 static uint8_t USBD_CUSTOMHID_GetClassDescriptor (
void *pudev,
USB_DEVICE_REQ *req);
33 static uint8_t USBD_CUSTOMHID_ClassReqHandle (
void *pudev,
USB_DEVICE_REQ *req);
34 static uint8_t USBD_CUSTOMHID_GetInterface (
void *pudev,
USB_DEVICE_REQ *req);
35 static uint8_t USBD_CUSTOMHID_SetInterface (
void *pudev,
USB_DEVICE_REQ *req);
36 static uint8_t USBD_CUSTOMHID_DataIn (
void *pudev, uint8_t EpID);
37 static uint8_t USBD_CUSTOMHID_DataOut (
void *pudev, uint8_t EpID);
38 static uint8_t* USBD_CUSTOMHID_GetCfgDesc (uint8_t Speed, uint16_t *len);
41 static void IAP_Req_ERASE (
void *pudev);
42 static void IAP_Req_DNLOAD (
void *pudev);
43 static void IAP_Req_OptionByte(
void *pudev);
44 static void IAP_Req_Leave (
void *pudev);
45 static void IAP_SendAddress (
void *pudev);
50 uint8_t Report_buf[65];
51 uint8_t OptionByte[17] = {0};
54 uint8_t DeviceStatus[17];
55 uint8_t BinAddress[17];
57 uint8_t USBD_CUSTOMHID_Report_ID = 0;
60 static uint32_t USBD_CUSTOMHID_AltSet = 0;
61 static uint32_t USBD_CUSTOMHID_Protocol = 0;
62 static uint32_t USBD_CUSTOMHID_IdleState = 0;
64 static uint16_t TransferTimes = 0;
65 static uint16_t PageCount = 0;
66 static uint32_t FileLength = 0;
67 static uint32_t BaseAddress = APP_LOADED_ADDR;
69 extern uint8_t MAL_Buffer[TRANSFER_SIZE];
74 USBD_CUSTOMHID_DeInit,
75 USBD_CUSTOMHID_GetClassDescriptor,
76 USBD_CUSTOMHID_ClassReqHandle,
77 USBD_CUSTOMHID_GetInterface,
78 USBD_CUSTOMHID_SetInterface,
81 USBD_CUSTOMHID_DataIn,
82 USBD_CUSTOMHID_DataOut,
84 USBD_CUSTOMHID_GetCfgDesc,
88 const uint8_t USBD_HID_CfgDesc[CUSTOMHID_CONFIG_DESC_SIZE] =
91 USB_DESCTYPE_CONFIGURATION,
92 CUSTOMHID_CONFIG_DESC_SIZE,
102 USB_DESCTYPE_INTERFACE,
119 CUSTOMHID_REPORT_DESC_SIZE,
124 USB_DESCTYPE_ENDPOINT,
132 USB_DESCTYPE_ENDPOINT,
135 CUSTOMHID_OUT_PACKET,
141 const uint8_t CustomHID_ReportDescriptor[CUSTOMHID_REPORT_DESC_SIZE] =
182 uint8_t USBD_CUSTOMHID_Init (
void *pudev, uint8_t ConfigIndex)
191 CUSTOMHID_IN_PACKET);
197 CUSTOMHID_OUT_PACKET);
203 USB_EP_Rx(pudev, CUSTOMHID_OUT_EP, Report_buf, 64);
214 uint8_t USBD_CUSTOMHID_DeInit (
void *pudev, uint8_t ConfigIndex)
232 uint8_t USBD_CUSTOMHID_ClassReqHandle (
void *pudev,
USB_DEVICE_REQ *req)
234 uint8_t USBD_CUSTOMHID_Report_LENGTH = 0;
236 switch (req->bRequest)
243 USB_CtlTx (pudev, (uint8_t *)&USBD_CUSTOMHID_IdleState, 1);
247 USB_CtlTx (pudev, (uint8_t *)&USBD_CUSTOMHID_Protocol, 1);
252 USBD_CUSTOMHID_Report_ID = (uint8_t)(req->wValue);
253 USBD_CUSTOMHID_Report_LENGTH = (uint8_t)(req->wLength);
254 USB_CtlRx (pudev, Report_buf, USBD_CUSTOMHID_Report_LENGTH);
258 USBD_CUSTOMHID_IdleState = (uint8_t)(req->wValue >> 8);
262 USBD_CUSTOMHID_Protocol = (uint8_t)(req->wValue);
279 uint8_t USBD_CUSTOMHID_GetClassDescriptor (
void *pudev,
USB_DEVICE_REQ *req)
282 uint8_t *pbuf = NULL;
284 switch(req->wValue >> 8)
286 case CUSTOMHID_REPORT_DESCTYPE:
287 len = MIN(CUSTOMHID_REPORT_DESC_SIZE, req->wLength);
288 pbuf = (uint8_t *)CustomHID_ReportDescriptor;
291 case CUSTOMHID_DESC_TYPE:
292 len = MIN(USB_CUSTOMHID_DESC_SIZE, req->wLength);
293 pbuf = (uint8_t *)USBD_HID_CfgDesc + 0x12;
311 uint8_t USBD_CUSTOMHID_GetInterface (
void *pudev,
USB_DEVICE_REQ *req)
313 USB_CtlTx (pudev, (uint8_t *)&USBD_CUSTOMHID_AltSet, 1);
324 uint8_t USBD_CUSTOMHID_SetInterface (
void *pudev,
USB_DEVICE_REQ *req)
326 USBD_CUSTOMHID_AltSet = (uint8_t)(req->wValue);
342 if (pudev->dev.device_cur_status == USB_STATUS_CONFIGURED)
344 USB_EP_Tx (pudev, CUSTOMHID_IN_EP, report, Len);
356 uint8_t *USBD_CUSTOMHID_GetCfgDesc (uint8_t Speed, uint16_t *len)
358 *len =
sizeof (USBD_HID_CfgDesc);
360 return (uint8_t*)USBD_HID_CfgDesc;
369 uint8_t USBD_CUSTOMHID_DataIn (
void *pudev, uint8_t EpID)
380 uint8_t USBD_CUSTOMHID_DataOut (
void *pudev, uint8_t EpID)
384 switch (Report_buf[0])
387 switch(Report_buf[1])
390 IAP_Req_DNLOAD(pudev);
394 IAP_Req_ERASE(pudev);
397 case IAP_OPTION_BYTE:
398 IAP_Req_OptionByte(pudev);
402 IAP_Req_Leave(pudev);
405 case IAP_GETBIN_ADDRESS:
406 IAP_SendAddress(pudev);
420 USB_EP_Rx(pudev, CUSTOMHID_OUT_EP, Report_buf, 64);
430 static void IAP_Req_DNLOAD(
void *pudev)
432 if(TransferTimes != 0)
434 if(TransferTimes == 1)
436 IAP_MAL_Write(&Report_buf[2], BaseAddress, FileLength % TRANSFER_SIZE);
438 DeviceStatus[0] = 0x02;
439 DeviceStatus[1] = 0x02;
446 BaseAddress += TRANSFER_SIZE;
458 static void IAP_Req_ERASE(
void *pudev)
460 BaseAddress = Report_buf[2];
461 BaseAddress |= Report_buf[3] << 8;
462 BaseAddress |= Report_buf[4] << 16;
463 BaseAddress |= Report_buf[5] << 24;
465 PageCount = Report_buf[6];
467 FileLength = Report_buf[7];
468 FileLength |= Report_buf[8] << 8;
469 FileLength |= Report_buf[9] << 16;
470 FileLength |= Report_buf[10] << 24;
472 if((FileLength % TRANSFER_SIZE) == 0)
474 TransferTimes = FileLength / TRANSFER_SIZE;
478 TransferTimes = FileLength / TRANSFER_SIZE + 1;
483 DeviceStatus[0] = 0x02;
484 DeviceStatus[1] = 0x01;
486 USB_EP_Tx(pudev, CUSTOMHID_IN_EP, DeviceStatus, 17);
494 static void IAP_Req_OptionByte(
void *pudev)
497 uint32_t Address = 0x1FFFF800;
501 for(i = 1; i < 17; i++)
503 OptionByte[i] = *(uint8_t *)Address;
515 static void IAP_Req_Leave(
void *pudev)
528 static void IAP_SendAddress(
void *pudev)
530 BinAddress[0] = 0x02;
532 BinAddress[1] = (uint8_t)(APP_LOADED_ADDR);
533 BinAddress[2] = (uint8_t)(APP_LOADED_ADDR >> 8);
534 BinAddress[3] = (uint8_t)(APP_LOADED_ADDR >> 16);
535 BinAddress[4] = (uint8_t)(APP_LOADED_ADDR >> 24);
USB device class callback type define.
uint8_t USBD_CUSTOMHID_SendReport(USB_DEVICE_HANDLE *pudev, uint8_t *report, uint16_t Len)
Send custom HID Report.
void USB_EP_Tx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to transmit data.
Custom HID device class core defines for usb iap.
uint8_t IAP_MAL_Erase(uint32_t Addr, uint32_t PageCount)
Erase a memory sector.
uint8_t IAP_MAL_Init(void)
Initialize the memory media on the GD32.
USB DFU device media access layer header 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 IAP_MAL_Write(uint8_t Data[], uint32_t Addr, uint32_t Len)
Write data to sectors of memory.
uint8_t USB_CtlTx(USB_CORE_HANDLE *pudev, uint8_t *pbuf, uint16_t Len)
Transmit data on the control pipe.
uint8_t IAP_MAL_DeInit(void)
Deinitialize the memory media on the GD32.
#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.