AN050 GD32 USBFS&USBHS Firmware Library User Guide Table of Contents Table of Contents ......................2 List of Figures ........................ 4 List of Tables ........................5 Introduction ......................7 Library architecture and file structure ..............8 2.1. Library Architecture ....................8 2.2.
Page 3
AN050 GD32 USBFS&USBHS Firmware Library User Guide 6.1.2. usb_conf.h ..........................49 6.2. Host VBUS Configuration ..................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.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Introduction This application note is provided specifically for the GD32 MCU Universal Serial Bus full- speed interface USBFS module. USBHS modules are the same as USBFS in general, corresponding difference between USBFS and USBHS will be marked in the article. The purpose of this note is to make it easier for customers to use the USBFS and USBHS firmware library and to use this library for project development faster.
GD32 USBFS&USBHS Firmware Library User Guide Library architecture and file structure Library Architecture 2.1. USBFS module firmware structure of GD32 series is shown in Figure 2-1. GD32 USBFS firmware library framework. The figure show USBFS host and device structure, user application call the USBFS firmware library to realize the USB data commuction.
AN050 GD32 USBFS&USBHS Firmware Library User Guide File structure 2.2. Take firmware library of GD32F4xx as an example, include the following 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.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 2-4. Driver 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 and standard enumeration file, which is called by host and device.
AN050 GD32 USBFS&USBHS Firmware Library User Guide USBFS bottom driver The file of USBFS bottom driver located in the driver folder. Bottom driver of whole firmware library is directly relate to USB hardware module, which include register read-write and FIFO operation, As shown in Table 3-1.
AN050 GD32 USBFS&USBHS Firmware Library User Guide USBFS middle layer driver The middle driver layer is divided into host middle layer and device middle layer. Device middle layer packages the transaction and basic function of USB device transfer. Host middle layer packages the transaction and basic function of USB host transfer.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Function name Functional description usbh_int_txfifoempty handle the TX FIFO empty interrupt Table 4-4. usbh_core.h/.c file function Function name Functional description usbh_init USB host stack initializations usbh_deinit de-initialize USB host usbh_class_register USB host register device class usbh_core_task USB host core main state machine process usbh_error_handler...
AN050 GD32 USBFS&USBHS Firmware Library User Guide Function name Functional description usbh_pipe_update modify a pipe usbh_pipe_allocate allocate a new pipe usbh_pipe_free free a pipe usbh_pipe_delete delete all USB host pipe usbh_freepipe_get get a free pipe number for allocation Table 4-7. usbh_transc.h/.c file function Function name Functional description usbh_ctlsetup_send...
AN050 GD32 USBFS&USBHS Firmware Library User Guide Function name Functional description usb_devaddr_set set the USB device address usb_oepintnum_read read device all OUT endpoint interrupt register usb_oepintr_read read device OUT endpoint interrupt flag register usb_iepintnum_read read device all IN endpoint interrupt register usb_rwkup_set set remote wakeup signalling usb_rwkup_reset...
AN050 GD32 USBFS&USBHS Firmware Library User Guide Function name Functional description _usb_std_reserved no operation, just for reserved _usb_dev_desc_get get the device descriptor _usb_config_desc_get get the configuration descriptor _usb_bos_desc_get get the BOS descriptor _usb_str_desc_get get string descriptor _usb_std_getstatus handle Get_Status request _usb_std_clearfeature handle USB Clear_Feature request _usb_std_setfeature...
AN050 GD32 USBFS&USBHS Firmware Library User Guide USBFS Device Library USBFS device library is based on the above underlying layer and middle layer driver, which include device library configuration, descriptor, interrupt, user interface, device class interface and example introduction. Device Library Configuration 5.1.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 5-1. Firmware library flowchart start Corresponding function USB clock configuration usb_rcu_config USB timer configuration usb_timer_init User interface function register hid_itfop_register Device class interface function register udev->dev.class_core = class_core; Get serial string serial_string_get USB basic / core initialization usb_basic_init / usb_core_init USB device disconnect...
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 5-2. Device class file path Descriptor mainly include device descriptor, configuration descriptor and string descriptor, etc. There is some specific descriptor for some device class, such as HID device support report descriptor, high speed device support other speed configuration descriptor and qualifier descriptor.
AN050 GD32 USBFS&USBHS Firmware Library User Guide [(uint8_t)USB_DESCTYPE_STR - 1U] = _usb_str_desc_get, #ifdef USE_USB_HS [(uint8_t)USB_DESCTYPE_DEV_QUALIFIER - 3U] = _usb_qualifier_desc_get, [(uint8_t)USB_DESCTYPE_OTHER_SPD_CONFIG - 3U] = _usb_other_speed_config_desc_get, #endif Interrupt handling 5.4. Table 5-3. USBFS device interruption. The interrupt of USBFS device interface is shown in Every interrupt flag corresponds to process handler of interrupt fuction, OEPIF, IEPIF and RXFNEIF flag is concerned about data transfer.
Page 23
AN050 GD32 USBFS&USBHS Firmware Library User Guide The interrupt handler function of OUT endpoint is shown as below: static uint32_t usbd_int_epout (usb_core_driver *udev) uint32_t epintnum = 0U; uint8_t ep_num = 0U; (epintnum usb_oepintnum_read (udev); epintnum; epintnum >>= 1, ep_num++) { (epintnum &...
Page 24
AN050 GD32 USBFS&USBHS Firmware Library User Guide return In OUT endpoint interrupt handler function, depending on the interrupt flag register, out_endp_intr function judge the event of OUT endpoint interrupt, which include transfer finished interrupt and setup phase finished interrupt. After corresponding OUT endpoint interrupt event generated, MCU execute the corresponding interrupt handler function through polling interrupt flag.
Page 25
AN050 GD32 USBFS&USBHS Firmware Library User Guide In interrupt handler function of IN endpoint, process transfer finished interrupt and transmit FIFO empty interrupt. After corresponding IN endpoint interrupt event generated, MCU execute the corresponding interrupt handler function through polling interrupt flag. The interrupt handler function of Rx FIFO non empty is shown as below: static uint32_t usbd_int_rxfifo (usb_core_driver...
AN050 GD32 USBFS&USBHS Firmware Library User Guide case RSTAT_SETUP_COMP: /* trigger the OUT endpoint interrupt */ break; case RSTAT_SETUP_UPDT: ((0U == transc->ep_addr.num) && == bcount) && (DPID_DATA0 == data_PID)) { /* copy the setup packet received in FIFO into the setup buffer in RAM */ (void)usb_rxfifo_read (&udev->regs, (uint8_t...
AN050 GD32 USBFS&USBHS Firmware Library User Guide usb_class_core usbd_hid_cb .command = NO_CMD, .alter_set = 0U, .init = hid_init, .deinit = hid_deinit, .req_proc = hid_req, .data_in hid_data_in The above initialization could implement initialization, deinitialization, device class request and data transfer of device class. Data transmission process 5.6.
AN050 GD32 USBFS&USBHS Firmware Library User Guide usbd_out_transc function. data_out callback function actually call the cdc_acm_out function. Once enter in cdc_acm_out function, it is indicated that some data are received by device, and then call function cdc_acm_data_receive, so as to prepare next data packet. uint8_t usbd_out_transc (usb_core_driver *udev,...
AN050 GD32 USBFS&USBHS Firmware Library User Guide Descriptor name Functional description usb_desc_input_terminal input terminal descriptor usb_desc_mono_feature_unit mono feature unit descriptor usb_desc_output_terminal output terminal descriptor usb_desc_AS_itf AS interface descriptor usb_desc_format_type format type descriptor usb_desc_std_ep standard endpoint descriptor usb_desc_AS_ep AS endpoint descriptor AUDIO device class interface AUDIO device class interface is shown as below struct, the function of struct is referred to Table 5-5.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Request name Functional description AUDIO_REQ_SET_MAX set maximum value request AUDIO_REQ_GET_MAX get maximum value request AUDIO_REQ_SET_RES set resolution request AUDIO_REQ_GET_RES get resolution request AUDIO user interface AUDIO user interface definition is shown as below struct. audio_fops_struct audio_out_fops init, deinit,...
AN050 GD32 USBFS&USBHS Firmware Library User Guide 1) data OUT phase Opening audio file in host is shown in Figure 5-6. Audio playback file, the playing audio file in host is heared through headphone which is connected to EVAL board. Figure 5-6.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 5-7. Audio system sound configuration Enter in “record” item, and double click on the microphone. In microphone attribute, select “monitor” interface, check “monitor this device” and select default playing device in “monitor” interface.
AN050 GD32 USBFS&USBHS Firmware Library User Guide CDC descriptor introduction Device descriptor include CDC device VID(0x28e9) and PID(0x018a). In configuration descriptor set, include CDC corresponding descriptor item, two interface, one command interface and one data interface, command interface corresponding descriptor is shown as Table 5-8.
AN050 GD32 USBFS&USBHS Firmware Library User Guide CDC device class request. Table 5-10. CDC device class request Request name Functional description SEND_ENCAPSULATED_COMMAND Not used GET_ENCAPSULATED_RESPONSE Not used SET_COMM_FEATURE Not used GET_COMM_FEATURE Not used CLEAR_COMM_FEATURE Not used SET_LINE_CODING set serial port parameters GET_LINE_CODING get serial port parameters SET_CONTROL_LINE_STATE...
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 5-10. Virtual serial data transmitting and receiving For mass data test, it is necessary to add send byte number, and configure Timing sending function in serial debugging assistant, which is shown in Figure 5-11.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 5-12. DFU state machine flow chart DFU_DETACH APP_IDLE APP_DETACH DETACH TIMEOUT Application Program Mode DFU Program Mode USB Reset Any status except OK Any State (except 0 or 1) DFU_ERROR State 2,3,5,6,9 USB reset Corrupt Power On reset...
AN050 GD32 USBFS&USBHS Firmware Library User Guide The functions of each function are shown in the following Table 5-15. DFU user interface functions: Table 5-15. DFU user interface functions Function name Functional description flash_if_init Memory medium interface initialization flash_if_deinit Memory medium interface deinitialization flash_if_erase Memory medium erase operation flash_if_write...
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 5-14. All in one connection 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 out “selected pages” interface, select the upload page and then get the corresponding data.
AN050 GD32 USBFS&USBHS Firmware Library User Guide 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 operation.
AN050 GD32 USBFS&USBHS Firmware Library User Guide 5.7.4. MSC device is mass storage device, include U-disk and CDROM. MSC descriptor introduction Device descriptor include MSC device VID(0x28e9) and PID(0x028f). In configuration descriptor set, include configure descriptor, interface descriptor and endpoint descriptor, which is shown as below.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 5-17. MSC device class In my computer, the newly added disk is visible, because disk is lack of file system, so it is necessary to format the disk firstly, which is shown in Figure 5-18.
AN050 GD32 USBFS&USBHS Firmware Library User Guide 5.7.5. HID device class is implement human-machine interaction interface, HID device has a wide usage range, not only include mouse, keyboard and touch device, but also include customed HID device. HID descriptor introduction Device descriptor include HID device VID(0x28e9) and PID(0x0380).
AN050 GD32 USBFS&USBHS Firmware Library User Guide Request name value Functional description GET_PROTOCOL 0x03 Get protocol SET_REPORT 0x09 Set report SET_IDLE 0x0A Set idle SET_PROTOCOL 0x0B Set protocol HID user interface HID user interface is enumerated as keyboard, which is shown in below structure. hid_fop_handler fop_handler .hid_itf_config = key_config,...
AN050 GD32 USBFS&USBHS Firmware Library User Guide fail. USB printer 5.7.6. Printer descriptor introduction Device descriptor include Printer device VID(0x28e9) and PID(0x028d). In printer configuration descriptor set, include configuration, interface and endpoint descriptor, corresponding descriptor is shown as below. Printer device class interface Printer device class interface is shown in below structure, and structure function is shown in below Table 5-23.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 6-1. Construct circuit through triode to control VBUS 10KΩ PD13 S8550 470R E4 16V/10uF,AVX +U5V USB_VBUS VBUS PA11 USB_DM PA12 USB_DP Shield OTG_FS 1MΩ 50V/4700pF As shown in Figure 6-1. Construct circuit through triode to control VBUS, PD13 is configured to be GPIO open drain mode(OD).
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 6-2. Control VBUS by Logic Chip Circuit 10KΩ PD13 OUTA VBUS_FS_5V 10KΩ FLGA +3V3 FLGB 10KΩ OUTB LED6 50V/0.1uF 16V/10uF,AVX SP2526A-1EN ENA and ENB active HIGH 470Ω LED RED R100 CPEN 10KΩ EXTVBUS RBIAS VBUS_HS_5V...
Page 53
AN050 GD32 USBFS&USBHS Firmware Library User Guide Interrupt Flag Description Operation Mode HCIF Host channels interrupt flag Host Mode HPIF Host port interrupt flag Host Mode ISOONCIF/PXNCIF Periodic transfer Not Complete Interrupt Host or device mode flag /Isochronous OUT transfer Not Complete Interrupt Flag NPTXFEIF Non-Periodic Tx FIFO empty interrupt...
AN050 GD32 USBFS&USBHS Firmware Library User Guide State Machine Process 6.4. Based on the below state machine, USB implement device operation, such as connecting, detecting and enumeration. state machine is loop executing in main function. Figure 6-3. USB host state machine Host start HOST_DEFAULT Detect device...
AN050 GD32 USBFS&USBHS Firmware Library User Guide usbh_user_device_reset, usbh_user_device_disconnected, usbh_user_over_current_detected, usbh_user_device_speed_detected, usbh_user_device_desc_available, usbh_user_device_address_assigned, usbh_user_configuration_descavailable, usbh_user_manufacturer_string, usbh_user_product_string, usbh_user_serialnum_string, usbh_user_enumeration_finish, usbh_user_userinput, usbh_usr_msc_application, usbh_user_device_not_supported, usbh_user_unrecovered_error The functions of each function are described in Table 6-4. USB host library user interface function. Table 6-4. USB host library user interface function Function name Functional description usbh_user_init...
AN050 GD32 USBFS&USBHS Firmware Library User Guide USB Host Library Device Class Interface 6.6. USB device class interface is implemented through the below structure. /* device class callbacks */ typedef struct uint8_t class_code; /*!< USB class type */ usbh_status (*class_init) (struct _usbh_host *phost);...
AN050 GD32 USBFS&USBHS Firmware Library User Guide usbh_hid_sof The initialization function of structure is shown in usbh_hid_core.c file, except for include structure initialization, and include other HID device class function and corresponding function, shown in Table 6-5. HID host class library function.
AN050 GD32 USBFS&USBHS Firmware Library User Guide MSC device class 6.6.2. The MSC device is initialized as follows: usbh_class usbh_msc USB_CLASS_MSC, usbh_msc_itf_init, usbh_msc_itf_deinit, usbh_msc_req, usbh_msc_handle, The initialization function of structure is shown in usbh_msc_core.c file, except for include structure initialization, and include other MSC device class function and corresponding function, shown in Table 6-6.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Device class File name Function name Description si.c device send 'Test unit ready' command usbh_msc_test_unitready to the device send the read capacity usbh_msc_read_capacity10 command to the device send the mode sense6 usbh_msc_mode_sense6 command to the device send the Request Sense usbh_msc_request_sense command to the device...
AN050 GD32 USBFS&USBHS Firmware Library User Guide Figure 6-5. Hid host routine operation diagram Figure 6-6. Routine for mouse-over display of HID host Figure 6-7. Routine for HID host keyboard display MSC HOST 6.7.2. MSC host rountine could be used to identify the U-disk, enumeration course and data transfer phase is displayed in display screen.
AN050 GD32 USBFS&USBHS Firmware Library User Guide The operation steps of MSC host rountine is shown as below figure, firstly, insert OTG cable into USB connector, and then download MSC_Host program file to EVAL board and run the application. If one U-disk is connected, U-disk enumeration information could be displayed on the display screen.
AN050 GD32 USBFS&USBHS Firmware Library User Guide Revision history Table 7-1. Revision history Revision No. Descriptor Date Initial Release Mar.28, 2022...
Page 65
Important Notice This document is the property of GigaDevice Semiconductor Inc. and its subsidiaries (the "Company"). This document, including any product of the Company described in this document (the “Product”), is owned by the Company under the intellectual property laws and treaties of the People’s Republic of China and other jurisdictions worldwide.
Need help?
Do you have a question about the GD32 Series and is the answer not in the manual?
Questions and answers