background image

 

 

 

 

 

 

 

 

GigaDevice Semiconductor Inc. 

 

GD32 USBFS&USBHS   

Firmware Library User Guide

 

 

 

 

 

 

 

Application Note 

AN050 

 

 

Summary of Contents for GD32 Series

Page 1: ...GigaDevice Semiconductor Inc GD32 USBFS USBHS Firmware Library User Guide Application Note AN050...

Page 2: ...4 2 Device middle layer driver function 15 5 USBFS Device Library 18 5 1 Device Library Configuration 18 5 1 1 usbd_conf h 18 5 1 2 usb_conf h 18 5 2 Firmware library process 19 5 3 Descriptor 20 5 4...

Page 3: ...uration 50 6 3 Interrupt handling 52 6 4 State Machine Process 56 6 5 USB Host Library User Interface 56 6 6 USB Host Library Device Class Interface 58 6 6 1 HID device class 58 6 6 2 MSC device class...

Page 4: ...5 11 Virtual serial port large data transmitting and receiving 36 Figure 5 12 DFU state machine flow chart 37 Figure 5 13 DFU device class 39 Figure 5 14 All in one connection 40 Figure 5 15 All in o...

Page 5: ...USBFS device interruption 22 Table 5 4 AUDIO relevant descriptors 29 Table 5 5 AUDIO device class interface function 30 Table 5 6 AUDIO device class request 30 Table 5 7 AUDIO user interface function...

Page 6: ...ble 6 2 usb_conf h Configuration description 50 Table 6 3 USBFS host interrupt 52 Table 6 4 USB host library user interface function 57 Table 6 5 HID host class library function 59 Table 6 6 MSC host...

Page 7: ...opment faster This application note is divided into four sections 1 The architecture and files of the library 2 Introduction to the bottom and middle layer driver functions of the library 3 Introducti...

Page 8: ...t modify driver layer which consist of host driver device driver and USB underlying layer As a portion of application layer USB application class file realize specific host or device application class...

Page 9: ...llowing four file folders Figure 2 2 USBFS Firmware Library Folder Device file folder include Protocol layer files and device class files which is required by USB device Figure 2 3 Device folder Drive...

Page 10: ...river folder Host folder include register definition bottom layer driver and USB interrupt handler which is required by USB host Figure 2 5 Host folder The folder ustd include common device class file...

Page 11: ...AN050 GD32 USBFS USBHS Firmware Library User Guide 11 Figure 2 6 Ustd folder...

Page 12: ...usb_core h c file function Table 3 2 usb_core h c file function Function name Functional description usb_core_reset config USB core to soft reset usb_basic_init config USB core basic usb_core_init in...

Page 13: ...vel driver drv_usbd_int h c USB device mode interrupt routines device core usbd_core h c USB device mode core functions usbd_enum h c USB enumeration function usbd_transc h c USB transaction core func...

Page 14: ...ice descriptor from the USB device usbh_cfgdesc_get get configuration descriptor from the USB device usbh_strdesc_get get string descriptor from the USB device usbh_setaddress set the address to the c...

Page 15: ...are a pipe and start a transfer 4 2 Device middle layer driver function Table 4 8 drv_usb_dev h c file function Function name Functional description usb_devcore_init initialize USB core registers for...

Page 16: ...terrupt usbd_int_enumfinish handle USB speed enumeration finish interrupt usbd_int_suspend USB suspend interrupt handler usbd_emptytxfifo_write check FIFO for the next packet to be loaded Table 4 10 u...

Page 17: ...equest _usb_std_setdescriptor handle USB Set_Descriptor request _usb_std_getconfiguration handle USB Get_Configuration request _usb_std_setconfiguration handle USB Set_Configuration request _usb_std_g...

Page 18: ...FG_MAX_NUM 1 define USBD_ITF_MAX_NUM 1 define USBD_HID_INTERFACE 0 USB user string supported define USB_SUPPORT_USER_STRING_DESC define HID_IN_EP EP1_IN define HID_IN_PACKET 8 Each configuration is de...

Page 19: ...SOF output USB_LOW_POWER enable low power mode VBUS_SENSING_ENABLED enable VBUS SENSING USE_HOST_MODE host mode USE_DEVICE_MODE device mode USE_OTG_MODE OTG mode Note 1 If user need to measure the SOF...

Page 20: ...it hid_itfop_register udev dev class_core class_core serial_string_get usb_basic_init usb_core_init usbd_disconnect USB device disconnect USB FIFO endpoint interrupt enable usb_devcore_init USB device...

Page 21: ...ack function static usb_reqsta _std_dev_req usb_core_driver udev usb_req req USB_GET_STATUS _usb_std_getstatus USB_CLEAR_FEATURE _usb_std_clearfeature USB_RESERVED2 _usb_std_reserved USB_SET_FEATURE _...

Page 22: ...DPSC ID pin status change Host or device mode LPMIF 1 LPM interrupt flag Host or device mode ISOONCIF PXNCIF Periodic transfer Not Complete Interrupt flag Isochronous OUT transfer Not Complete Interru...

Page 23: ...ep_num DOEPINTF DOEPINTF_TF if uint8_t USB_USE_DMA udev bp transfer_mode __IO uint32_t eplen udev regs er_out ep_num DOEPLEN udev dev transc_out ep_num xfer_count udev dev transc_out ep_num max_len e...

Page 24: ...f IN endpoint is shown as below static uint32_t usbd_int_epin usb_core_driver udev uint32_t epintnum 0U uint8_t ep_num 0U for epintnum usb_iepintnum_read udev epintnum epintnum 1 ep_num if epintnum 0x...

Page 25: ...8_t data_PID 0U uint32_t bcount 0U __IO uint32_t devrxstat 0U disable the Rx status queue non empty interrupt udev regs gr GINTEN GINTEN_RXFNEIE get the status from the top of the FIFO devrxstat udev...

Page 26: ...enable the Rx status queue level interrupt udev regs gr GINTEN GINTEN_RXFNEIE return 1U In interrupt handler function of Rx FIFO non empty mainly process FIFO data receiving include SETUP transaction...

Page 27: ...ev uint8_t ep_num device data out handler uint8_t SOF usb_dev udev Start of frame handler uint8_t incomplete_isoc_in usb_dev udev Incomplete synchronization IN transfer handler uint8_t incomplete_isoc...

Page 28: ...transfer process of USB data in GD FS library 5 6 1 IN direction In data transfer phase non zero endpoint data of IN direction is processed in usbd_in_transc function In following figure data_in callb...

Page 29: ...ss_core data_out udev ep_num else no operation return uint8_t USBD_OK 5 7 USB device class routine 5 7 1 AUDIO AUDIO device include speaker and micphone interface which could be selected in project co...

Page 30: ...roc audio_req_handler ctlx_out audio_ctlx_out data_in audio_data_in data_out audio_data_out SOF usbd_audio_sof Table 5 5 AUDIO device class interface function Descriptor name Functional description au...

Page 31: ...te_ctl periodic_tc get_state Corresponding function is shown as Table 5 7 AUDIO user interface functions Table 5 7 AUDIO user interface functions Function name Functional description init initialize A...

Page 32: ...ng audio file in host is heared through headphone which is connected to EVAL board Figure 5 6 Audio playback file 2 data IN phase Click the loudspeaker in bottom right corner of desktop and click volu...

Page 33: ...the sound which is transmitted from MCU device to host could be heared by default playing device As shown in Figure 5 8 Audio recording listening configuration Figure 5 8 Audio recording listening co...

Page 34: ...nction descriptor CDC device class interface The CDC device class interface is shown in the following structure whose function implementation is shown in the following Table 5 9 CDC device class inter...

Page 35: ...a transfer uint8_t cdc_acm_check_ready usb_dev udev send CDC ACM data void cdc_acm_data_send usb_dev udev receive CDC ACM data void cdc_acm_data_receive usb_dev udev The functions are shown in the fol...

Page 36: ...al debugging assistant which is shown in Figure 5 11 Virtual serial port large data transmitting and receiving Figure 5 11 Virtual serial port large data transmitting and receiving 5 7 3 DFU DFU is sp...

Page 37: ...rame DFU_DOWNLOAD wLenth 0 bitcanDownload 1 DFU_GETSTATUS Block complete DFU_DOWNLOAD wLenth 0 DFU_GETSTATUS Block in process Status Poll Timeout Status Poll Timeout bitManifestationTolerant 1 Status...

Page 38: ...clude device class request is shown in Table 5 14 DFU device class request Table 5 14 DFU device class request Request name value Functional description DFU_DETACH 0 DFU detach DFU_DNLOAD 1 Download D...

Page 39: ...eration flash_if_write Memory medium write operation flash_if_read Memory medium read operation flash_if_checkaddr Memory medium address legal check FLASH_IF_STRING Interface string DFU Routine operat...

Page 40: ...1 Download Select the target file and configure the corresponding download address after downloading reset the chip and then execute application 2 Upload Select the target file click OK and then pop...

Page 41: ...Figure 5 15 All in one uploading 3 Option Byte operation Double click Edit Option Bytes and then pop out the option byte corresponding information which is shown as Figure 5 16 All in one Option Byte...

Page 42: ...einit req_proc msc_core_req data_in msc_core_in data_out msc_core_out Table 5 16 MSC device class interface functions Function name Functional description msc_core_init Initialize MSC device msc_core_...

Page 43: ...ariable name Functional description STORAGE_Init Memory medium interface initialization STORAGE_IsReady Check whether memory medium is ready STORAGE_IsWriteProtected Check whether memory medium is wri...

Page 44: ...isk firstly which is shown in Figure 5 18 MSC device formatting Figure 5 18 MSC device formatting After formatting is finished write test is operated through copy the test file to disk after copy the...

Page 45: ...tor hid_report_desc report descriptor HID device class interface HID device class interface is shown in below structure and structure function is shown in below Table 5 20 HID device class interface f...

Page 46: ...nctions Function variable name Functional description key_config Key configuration hid_key_data_send Send key value HID routine operation guide Download the HID rountine to EVAL board a newly added HI...

Page 47: ...inter_out Table 5 23 printer device class interface function Function name Functional description printer_init Initialize printer device printer_deinit Deinitialize printer device printer_req Printer...

Page 48: ...ry User Guide 48 function Printer routine operation guide Download the Printer rountine to EVAL board a newly added printer device is visible in device manager As shown in Figure 5 21 printer device c...

Page 49: ...figuration name Functional description USBH_MAX_EP_NUM Maximum number of endpoints USBH_MAX_INTERFACES_NUM Maximum number of interfaces USBH_MAX_ALT_SETTING Maximum number of alternate interfaces USBH...

Page 50: ...ZE Received FIFO size USB_HTX_NPFIFO_FS_SIZE Non periodic transmit FIFO size USB_HTX_PFIFO_FS_SIZE periodic transmit FIFO size USB_ULPI_PHY_ENABLED Enable ULPI PHY USB_EMBEDDED_PHY_ENABLED Enable embe...

Page 51: ...0V 4700pF 5V USB_VBUS USB_DM USB_DP VBUS 1 DM 2 DP 3 ID 4 GND 5 Shield 6 USB_MiniAB receptacle CN3 OTG_FS U5V PA9 PA11 PA12 As shown in Figure 6 1 Construct circuit through triode to control VBUS PD13...

Page 52: ...AVX VBUS_HS_5V PD13 VBUS_FS_5V R96 10K GND R97 10K ENA and ENB active HIGH As shown in Figure 6 2 Control VBUS by Logic Chip Circuit PD13 is configured to be GPIO push pull output mode PP Enable USB...

Page 53: ...in below function static uint32_t usbh_int_rxfifonoempty usb_core_driver udev uint32_t count 0U __IO uint8_t pp_num 0U __IO uint32_t rx_stat 0U disable the RX status queue level interrupt udev regs gr...

Page 54: ...e count 0U rx_stat udev regs gr GRSTATP count break case GRXSTS_PKTSTS_CH_HALTED break default break enable the RX status queue level interrupt udev regs gr GINTEN GINTEN_RXFNEIE return 1U Data transm...

Page 55: ...fer_len len uint16_t txfifostate TFQSTAT_TXFS 4U if len udev host pipe pp_num xfer_len last packet len uint16_t udev host pipe pp_num xfer_len if PIPE_NON_PERIOD pp_mode udev regs gr GINTEN GINTEN_NPT...

Page 56: ...T_ENUM HOST_SET_WAKEUP_FEATURE HOST_CHECK_CLASS HOST_USER_INPUT HOST_ERROR HOST_CLASS_ENUM HOST_CLASS_HANDLER Host start Detect device Detect device speed Standard enumeration Configure wakeup Device...

Page 57: ...on of device resetting usbh_user_device_disconnected user operation of USB disconnection usbh_user_over_current_detected user operation of device overloading usbh_user_device_speed_detected user opera...

Page 58: ...machine struct _usbh_host phost usbh_status class_sof struct _usbh_host uhost void class_data usbh_class The structure initialization is implemented separately by each device class Interface file of e...

Page 59: ...he host pipes used for the HID class usbh_hid_itf_init initialize the hid class usbh_hid_class_req handle HID class requests for HID class usbh_hid_handle manage state machine for HID data transfers u...

Page 60: ...ocess manage the different states of BOT transfer and updates the status to upper layer usbh_msc_bot_abort manages the different error handling for stall usbh_msc_bot_reset reset msc bot request usbh_...

Page 61: ...7 USB Host Library Routine 6 7 1 HID HOST HID host rountine could be used to identify the keyboard and mouse enumeration course and data transfer phase is displayed in display screen Firstly insert ca...

Page 62: ...ID Host Support Mouse Keyboard Display the typed character Move mouse Click button Input character Figure 6 6 Routine for mouse over display of HID host Figure 6 7 Routine for HID host keyboard displa...

Page 63: ...tion could be displayed on the display screen sceondly press TAMPER key U disk root contents could be displayed Then press WAKEUP key write the file to U disk finally the information which indicate MS...

Page 64: ...AN050 GD32 USBFS USBHS Firmware Library User Guide 64 7 Revision history Table 7 1 Revision history Revision No Descriptor Date 1 0 Initial Release Mar 28 2022...

Page 65: ...ed for ordinary business industrial personal and or household applications only The Products are not designed intended or authorized for use as components in systems designed or intended for the opera...

Reviews: