31 static uint8_t USBD_DFU_Init (
void *pudev, uint8_t ConfigIndex);
32 static uint8_t USBD_DFU_DeInit (
void *pudev, uint8_t ConfigIndex);
33 static uint8_t USBD_DFU_GetClassDescriptor (
void *pudev,
USB_DEVICE_REQ *req);
34 static uint8_t USBD_DFU_ClassReqHandle (
void *pudev,
USB_DEVICE_REQ *req);
35 static uint8_t USBD_DFU_GetInterface (
void *pudev,
USB_DEVICE_REQ *req);
36 static uint8_t USBD_DFU_SetInterface (
void *pudev,
USB_DEVICE_REQ *req);
37 static uint8_t USBD_DFU_EP0_TxSent (
void *pudev);
38 static uint8_t USBD_DFU_EP0_RxReady (
void *pudev);
39 static uint8_t* USBD_DFU_GetCfgDesc (uint8_t USBSpeed, uint16_t *len);
40 static uint8_t* USBD_DFU_GetUsrStringDesc (uint8_t USBSpeed, uint8_t index, uint16_t *len);
46 static void DFU_Req_GETSTATUS (
void *pudev);
47 static void DFU_Req_CLRSTATUS (
void *pudev);
48 static void DFU_Req_GETSTATE (
void *pudev);
49 static void DFU_Req_ABORT (
void *pudev);
50 static void DFU_LeaveDFUMode (
void *pudev);
55 extern const uint8_t* USBD_DFU_StringDesc[];
56 extern uint8_t MAL_Buffer[];
59 uint8_t DeviceStatus[6] =
69 uint8_t Manifest_State = Manifest_complete;
72 static uint16_t BlockNum = 0;
73 static uint16_t Length = 0;
74 static uint32_t BaseAddress = APP_LOADED_ADDR;
75 static __IO uint32_t USBD_DFU_AltSet = 0;
82 USBD_DFU_GetClassDescriptor,
83 USBD_DFU_ClassReqHandle,
84 USBD_DFU_GetInterface,
85 USBD_DFU_SetInterface,
92 USBD_DFU_GetUsrStringDesc,
96 const uint8_t USBD_DFU_CfgDesc[USB_DFU_CONFIG_DESC_SIZE] =
99 USB_DESCTYPE_CONFIGURATION,
100 USB_DFU_CONFIG_DESC_SIZE,
111 #if (USBD_ITF_MAX_NUM > 1)
116 #if (USBD_ITF_MAX_NUM > 2)
121 #if (USBD_ITF_MAX_NUM > 3)
126 #if (USBD_ITF_MAX_NUM > 4)
131 #if (USBD_ITF_MAX_NUM > 5)
136 #if (USBD_ITF_MAX_NUM > 6)
137 #error "ERROR: usbd_dfu_core.c: Modify the file to support more descriptors!"
152 LOWBYTE(TRANSFER_SIZE),
153 HIGHBYTE(TRANSFER_SIZE),
173 static uint8_t USBD_DFU_Init (
void *pudev, uint8_t ConfigIndex)
187 static uint8_t USBD_DFU_DeInit (
void *pudev, uint8_t ConfigIndex)
190 DeviceStatus[0] = STATUS_OK;
191 DeviceStatus[4] = STATE_dfuIDLE;
208 static uint8_t USBD_DFU_GetClassDescriptor (
void *pudev,
USB_DEVICE_REQ *req)
211 uint8_t *pbuf = NULL;
213 if((req->wValue >> 8) == DFU_DESC_TYPE)
215 pbuf = (uint8_t*)USBD_DFU_CfgDesc + 9 + (9 * USBD_ITF_MAX_NUM);
216 len = MIN(USB_DFU_DESC_SIZE, req->wLength);
230 static uint8_t USBD_DFU_ClassReqHandle (
void *pudev,
USB_DEVICE_REQ *req)
232 switch (req->bRequest)
235 DFU_Req_DNLOAD(pudev, req);
239 DFU_Req_UPLOAD(pudev, req);
243 DFU_Req_GETSTATUS(pudev);
247 DFU_Req_CLRSTATUS(pudev);
251 DFU_Req_GETSTATE(pudev);
255 DFU_Req_ABORT(pudev);
259 DFU_Req_DETACH(pudev, req);
276 static uint8_t USBD_DFU_GetInterface (
void *pudev,
USB_DEVICE_REQ *req)
278 USB_CtlTx (pudev, (uint8_t *)&USBD_DFU_AltSet, 1);
289 static uint8_t USBD_DFU_SetInterface (
void *pudev,
USB_DEVICE_REQ *req)
291 if ((uint8_t)(req->wValue) < USBD_ITF_MAX_NUM)
293 USBD_DFU_AltSet = (uint8_t)(req->wValue);
308 static uint8_t USBD_DFU_EP0_TxSent (
void *pudev)
312 if (DeviceStatus[4] == STATE_dfuDNBUSY)
317 if ((MAL_Buffer[0] == GET_COMMANDS) && (Length == 1))
319 else if ((MAL_Buffer[0] == SET_ADDRESS_POINTER) && (Length == 5))
321 BaseAddress = MAL_Buffer[1];
322 BaseAddress |= MAL_Buffer[2] << 8;
323 BaseAddress |= MAL_Buffer[3] << 16;
324 BaseAddress |= MAL_Buffer[4] << 24;
326 else if ((MAL_Buffer[0] == ERASE) && (Length == 5))
328 BaseAddress = MAL_Buffer[1];
329 BaseAddress |= MAL_Buffer[2] << 8;
330 BaseAddress |= MAL_Buffer[3] << 16;
331 BaseAddress |= MAL_Buffer[4] << 24;
340 else if (BlockNum > 1)
343 Addr = (BlockNum - 2) * TRANSFER_SIZE + BaseAddress;
354 DeviceStatus[4] = STATE_dfuDNLOAD_SYNC;
361 else if (DeviceStatus[4] == STATE_dfuMANIFEST)
364 DFU_LeaveDFUMode(pudev);
375 static uint8_t USBD_DFU_EP0_RxReady (
void *pudev)
388 switch (DeviceStatus[4])
391 case STATE_dfuDNLOAD_SYNC:
392 case STATE_dfuDNLOAD_IDLE:
393 case STATE_dfuMANIFEST_SYNC:
394 case STATE_dfuUPLOAD_IDLE:
395 DeviceStatus[0] = STATUS_OK;
399 DeviceStatus[4] = STATE_dfuIDLE;
411 if ((USBD_DFU_CfgDesc[12 + (9 * USBD_ITF_MAX_NUM)]) & DFU_DETACH_MASK)
420 USB_HWP_mDelay (req->wValue);
433 if (req->wLength > 0)
435 if ((DeviceStatus[4] == STATE_dfuIDLE) || (DeviceStatus[4] == STATE_dfuDNLOAD_IDLE))
438 BlockNum = req->wValue;
439 Length = req->wLength;
441 DeviceStatus[4] = STATE_dfuDNLOAD_SYNC;
444 USB_CtlRx (pudev, (uint8_t*)MAL_Buffer, Length);
455 if ((DeviceStatus[4] == STATE_dfuDNLOAD_IDLE) || (DeviceStatus[4] == STATE_dfuIDLE))
457 Manifest_State = Manifest_In_Progress;
461 DeviceStatus[4] = STATE_dfuMANIFEST_SYNC;
478 uint8_t *Phy_Addr = NULL;
482 if (req->wLength > 0)
484 if ((DeviceStatus[4] == STATE_dfuIDLE) || (DeviceStatus[4] == STATE_dfuUPLOAD_IDLE))
487 BlockNum = req->wValue;
488 Length = req->wLength;
499 DeviceStatus[4] = (Length > 3) ? STATE_dfuIDLE : STATE_dfuUPLOAD_IDLE;
502 MAL_Buffer[0] = GET_COMMANDS;
503 MAL_Buffer[1] = SET_ADDRESS_POINTER;
504 MAL_Buffer[2] = ERASE;
507 USB_CtlTx (pudev, (uint8_t *)(&(MAL_Buffer[0])), 3);
509 else if (BlockNum > 1)
511 DeviceStatus[4] = STATE_dfuUPLOAD_IDLE;
514 Addr = ((BlockNum - 2) * TRANSFER_SIZE) + BaseAddress;
524 DeviceStatus[4] = STATUS_errSTALLEDPKT;
542 DeviceStatus[4] = STATE_dfuIDLE;
551 static void DFU_Req_GETSTATUS (
void *pudev)
553 switch (DeviceStatus[4])
555 case STATE_dfuDNLOAD_SYNC:
558 DeviceStatus[4] = STATE_dfuDNBUSY;
561 if(MAL_Buffer[0] == ERASE)
573 DeviceStatus[4] = STATE_dfuDNLOAD_IDLE;
580 case STATE_dfuMANIFEST_SYNC:
581 if (Manifest_State == Manifest_In_Progress)
583 DeviceStatus[4] = STATE_dfuMANIFEST;
588 else if ((Manifest_State == Manifest_complete) && \
589 ((USBD_DFU_CfgDesc[(11 + (9 * USBD_ITF_MAX_NUM))]) & 0x04))
591 DeviceStatus[4] = STATE_dfuIDLE;
603 USB_CtlTx (pudev, (uint8_t *)(&(DeviceStatus[0])), 6);
611 static void DFU_Req_CLRSTATUS (
void *pudev)
613 if (DeviceStatus[4] == STATE_dfuERROR)
615 DeviceStatus[0] = STATUS_OK;
616 DeviceStatus[4] = STATE_dfuIDLE;
621 DeviceStatus[0] = STATUS_errUNKNOWN;
622 DeviceStatus[4] = STATE_dfuERROR;
636 static void DFU_Req_GETSTATE (
void *pudev)
647 static void DFU_Req_ABORT (
void *pudev)
649 switch (DeviceStatus[4])
652 case STATE_dfuDNLOAD_SYNC:
653 case STATE_dfuDNLOAD_IDLE:
654 case STATE_dfuMANIFEST_SYNC:
655 case STATE_dfuUPLOAD_IDLE:
656 DeviceStatus[0] = STATUS_OK;
660 DeviceStatus[4] = STATE_dfuIDLE;
677 void DFU_LeaveDFUMode (
void *pudev)
679 Manifest_State = Manifest_complete;
685 if ((USBD_DFU_CfgDesc[(11 + (9 * USBD_ITF_MAX_NUM))]) & 0x04)
687 DeviceStatus[4] = STATE_dfuMANIFEST_SYNC;
691 DeviceStatus[4] = STATE_dfuMANIFEST_WAIT_RESET;
706 static uint8_t *USBD_DFU_GetCfgDesc (uint8_t USBSpeed, uint16_t *len)
708 *len =
sizeof (USBD_DFU_CfgDesc);
710 return (uint8_t*)USBD_DFU_CfgDesc;
720 static uint8_t* USBD_DFU_GetUsrStringDesc (uint8_t USBSpeed, uint8_t index, uint16_t *len)
723 if (index <= (USBD_INTERFACE_STR_IDX + USBD_ITF_MAX_NUM))
725 USBD_GetUnicodeString ((uint8_t *)USBD_DFU_StringDesc[index - USBD_INTERFACE_STR_IDX - 1], USBD_StrDesc, len);
uint8_t DFU_MAL_Write(uint32_t Addr, uint32_t Len)
Write data to sectors of memory.
USB device class callback type define.
void USBD_GetUnicodeString(uint8_t *desc, uint8_t *unicode, uint16_t *len)
Convert normal string into unicode one.
uint8_t DFU_MAL_GetStatus(uint32_t Addr, uint8_t Cmd, uint8_t *buffer)
Get the status of a given memory and store in buffer.
uint8_t DFU_MAL_DeInit(void)
Deinitialize the memory media on the GD32.
Header file for the usbd_dfu_core.c file.
uint8_t DFU_MAL_Init(void)
Initialize the memory media on the GD32.
void USBD_Disconnect(USB_DEVICE_HANDLE *pudev)
Configure usb device to be unconnected.
uint8_t DFU_MAL_Erase(uint32_t Addr)
Erase a memory sector.
uint8_t * DFU_MAL_Read(uint32_t Addr, uint32_t Len)
Read data from sectors of memory.
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.
void USBD_Connect(USB_DEVICE_HANDLE *pudev)
Configure usb device to be connected.
void USBD_EnumError(USB_DEVICE_HANDLE *pudev, USB_DEVICE_REQ *req)
Handle usb enumeration error event.
USB standard device request struct.