GD32 USBFS USBHS Firmware Library User Guide Rev1.0
GD32 USBFS USBHS Firmware Library User Guide Rev1.0
(Apr. 2019 )
GD32 USBFS/HS Firmware Library User Guide
Table of Contents
Table of Contents ........................................................................................................... 2
List of Figures ................................................................................................................ 3
List of Table .................................................................................................................... 4
1. USBFS/USBHS ......................................................................................................... 5
1.1. Brief ................................................................................................................................... 5
1.2. USBFS/USBHS principle brief ........................................................................................ 5
1.2.1. USBFS interface principle ....................................................................................................... 5
1.2.2. USBHS interface principle ...................................................................................................... 8
2
GD32 USBFS/HS Firmware Library User Guide
List of Figures
Figure 1-1. USBFS structure diagram ................................................................................................. 6
Figure 1-2. USBFS connect diagram as host or device .................................................................. 7
Figure 1-3. USBFS connection diagram as OTG device................................................................. 8
Figure 1-4. USBHS structure diagram ................................................................................................. 9
Figure 1-5. External ULPI PHY connection diagram........................................................................ 9
Figure 1-6. GD32F4xx series MCU USBFS/USBHS structure...................................................... 10
Figure 1-7. User callback function structure ................................................................................... 17
Figure 1-8. USBFS host state machine lookup table..................................................................... 19
Figure 1-9. USBFS host state machine flow diagram ................................................................... 20
Figure 1-10. Enumeration state machine handle lookup table ................................................... 20
Figure 1-11. Enumeration state machine flow diagram ................................................................ 21
Figure 1-12. Control transfer state machine handle lookup table ............................................. 22
Figure 1-13. Control transfer state machine flow diagram .......................................................... 22
Figure 1-14. OUT endpoint interrupt handle function ................................................................... 24
Figure 1-15. IN endpoint interrupt handle function ....................................................................... 25
Figure 1-16. USBFS demo diagram .................................................................................................... 26
Figure 1-17. USBFS host U disk main function .............................................................................. 27
Figure 1-18. USBFS host U disk enumeration information.......................................................... 28
Figure 1-19. USBFS host connect U disk routine result............................................................... 29
Figure 1-20. USBFS main function ..................................................................................................... 30
Figure 1-21. HID device experiment result ....................................................................................... 31
3
GD32 USBFS/HS Firmware Library User Guide
List of Table
Table 1-1. USB_FS characteristic ......................................................................................................... 6
Table 1-2. USB bottom layer file table ............................................................................................... 11
Table 1-3. usb_core.h/.c file function table ...................................................................................... 11
Table 1-4. USB_Host middle layer file ............................................................................................... 11
Table 1-5. usbh_core.h/.c file function table ................................................................................... 12
Table 1-6. usbh_ctrl.h/.c file function ................................................................................................ 12
Table 1-7. usbh_hcs.h/.c file function table ..................................................................................... 13
Table 1-8. usbh_int.h/.c file function table ....................................................................................... 13
Table 1-9. usbh_std.h/.c file function table ...................................................................................... 13
Table 1-10. USB_Device middle layer file ......................................................................................... 14
Table 1-11. usbd_core.h/.c file function table ................................................................................. 14
Table 1-12. usbd_int.h/.c file function table ..................................................................................... 15
Table 1-13. usbd_std.h/.c file function table.................................................................................... 15
Table 1-14. Application interface layer file table ............................................................................ 16
Table 1-15. Main.c file function table ................................................................................................. 16
Table 1-16. usr_cb user callback function structure function .................................................... 17
Table 1-17. usb_delay.c file function table ...................................................................................... 17
Table 1-18. HID/MSC host class library function ............................................................................ 18
Table 1-19. USB globle interrupt......................................................................................................... 22
Table 2-1. Revision history ................................................................................................................... 32
4
GD32 USBFS/HS Firmware Library User Guide
1. USBFS/USBHS
1.1. Brief
Basing on universal serial bus full speed / high speed interface structure, this article
introduce the interface principle and firmware structure of USBFS/USBHS, briefly describe
the firmware function performance, host state machine and USB interrupt mechanism.
Taking the MSC host and HID device for example, the article show the USB host and device
demo implementation.
As device, USBFS only support FS (12Mbps) device, meanwhile, as host, USBFS support
FS (12Mbps) and LS (1.5Mbps) device. To be dual role device, USBFS could be configured
as host or device. In OTG mode, the switch between host and device should conform to SRP
protocol and HNP protocol.
USBHS not only support all common characteristic and function, which USBFS support, but
also support USB2.0 HS (480Mbps) host and device.
USBFS structure
As the block diagram of Figure 1-1. USBFS structure diagram shown, Cortex-M core read
and write USBFS module register through AHB slave bus. USBFS register generate USB
interrupt for NVIC. There is 1.25KB data FIFO for USBFS, which is connected to SIE. In
device mode, FIFO is compose of one receive FIFO and multiple FIFOs. Every IN endpoint
own its transmit FIFO, and all OUT endpoints share receive FIFO. In host mode, data FIFO
divide into three parts, one is receive FIFO which is used to receive data, one is periodical
transmit FIFO which is used to transmit periodical data, and the other one is non-periodical
transmit FIFO which is used to transmit non-periodical data. All IN channel share receive
FIFO, all periodical OUT channel share periodical transmit FIFO and all non-periodical OUT
channel share non-periodical transmit FIFO. Data FIFO is connected to SIE. USB clock
domain comes from internal 48MHz clock. Through UIMI, USBFS controller is connected to
USBFS PHY, which is used to realize USB communication and compose of USB receiver
and USB interface circuit.
5
GD32 USBFS/HS Firmware Library User Guide
Figure 1-1. USBFS structure diagram
USB Interrupt
Register
USBFS host or device connection diagram is shown in Figure 1-2. USBFS connect
diagram as host or device. If USBFS is configured as host, it should supply 5V power, in
this case, 5V power is external USB device power supply.
4, force device mode bit of USB global configuration register is set, just only treat USB as
device.
VDD
USBFS
5V Supply Power
(necessary in host
GPIO
mode)
USB A/B connector
VBUS VBUS
DM DM
DP
DP
GND
USBFS OTG connection diagram is shown in Figure 1-3. USBFS connection diagram as
OTG device. Except common four wires in USB, ID wire, which indicates role of USBFS, is
necessary. If USB cable B port is connected, its ID wire is floating, high level of ID wire will
be detected, and USBFS is configured as slave device default. If USB cable A port is
7
GD32 USBFS/HS Firmware Library User Guide
connected, its ID wire is on the ground, USBFS will generate ID wire status change interrupt
to initial host software, and switch to host automatically.
GPIO port is used to control generating 5V power supply, and this power is input from
external, to be USBFS A device, supply power for other device and MCU. VBUS pin is used
to detect VBUS voltage.
USBHS interface module include an embedded USBFS PHY, the function of this PHY is
stronger than the function of USBFS module, it could support 6 bidirectional device
endpoints, 12 host channels and 4K bytes FIFO, thus, USBHS realize full speed and low
speed by USBFS PHY. Moreover, if ULPI interface is connected to USBHS, it could realize
USB high speed communication, its data transfer rate is up to 480Mbps. When USBHS is in
high speed host mode, USBHS support hub connection. In addition, there is DMA engine in
USBHS, which could accelerate data transfer rate between USBHS and system.
USBHS structure
8
GD32 USBFS/HS Firmware Library User Guide
Figure 1-4. USBHS structure diagram
USB interrupt
USBHS provide an ULPI interface to external PHY, if USBHS module try to realize USBHS
application, external HS ULPI PHY is necessary. Combining external ULPI PHY, USBHS
support high speed host and device, meanwhile, support all modes which USBFS PHY
support.
Software need clearing EMBPHY bit of USBHS_GUSBCS register to enable ULPI interface.
When ULPI mode enable, 60MHz clock need be input from ULPI_CLK pin. Software could
open or close 60MHz ULPI clock in RCU register.
5V Supply Power
if necessary
USBHS GPIO
USB Micro/Mini
ULPI_D[7:0]
connector
ULPI_DIR VBUS
External
DP
ULPI_NXT ULPI PHY DM
ULPI_STP ID
ULPI_CLK
9
GD32 USBFS/HS Firmware Library User Guide
1.3. USBFS/USBHS module firmware
GD32F4xx series MCU interface module structure diagram is shown in Figure 1-6.
GD32F4xx series MCU USBFS/USBHS structure. The figure show USBFS/USBHS host
and device structure. User application call GD32 USB firmware library to realize USB data
communication. The bottom of structure is hardware of MCU evaluation board. There is
three layers in GD32 USBFS/USBHS firmware library. The top is application interface layer,
user could change. Middle layer is USB host or USB device, the bottom layer is USB drivers.
Middle layer and bottom layer are called as “USB firmware library driver”, user could not
modify it. USB class file, which implement USB host application class or USB application
device class file, is part of application layer.
USB Driver underlying file include two files, which are shown in Table 1-2. USB bottom
layer file table.
10
GD32 USBFS/HS Firmware Library User Guide
Table 1-2. USB bottom layer file table
File name Tips
usb_core.h/.c USB core driver
usb_reg.h USB register operation
USB_Host middle layer include 5 files, which are shown in Table 1-4. USB_Host middle
layer file.
12
GD32 USBFS/HS Firmware Library User Guide
Function name Functional description
ctrl_state_polling_fun Control transfer state machine polling function
ctrl_idle_handle CTRL_IDLE state machine handle function
ctrl_setup_handle CTRL_SETUP state machine handle function
ctrl_data_handle CTRL_DATA state machine handle function
ctrl_status_handle CTRL_STATUS state machine handle function
ctrl_error_handle CTRL_ERROR state machine handle function
ctrl_stalled_handle CTRL_STALLED state machine handle function
ctrl_complete_handle CTRL_COMPLETE state machine handle function
usbh_xfer transmit data form host port
usbh_ctltx_setup Transmit SETUP token packet to device
hcd_submit_request prepare channel, start once transfer
13
GD32 USBFS/HS Firmware Library User Guide
enum_idle_handle ENUM_IDLE state handle function
ENUM_GET_FULL_DEV_DESC state handle
enum_get_full_dev_desc_handle
function
enum_set_addr_handle ENUM_SET_ADDR state handle function
enum_get_cfg_desc_handle ENUM_GET_CFG_DESC state handle function
ENUM_GET_FULL_CFG_DESC state handle
enum_get_full_cfg_desc_handle
function
ENUM_GET_MFC_STRING_DESC state handle
enum_get_mfc_string_desc_handle
function
ENUM_GET_PRODUCT_STRING_DESC state
enum_get_product_string_desc_handle
handle function
ENUM_GET_SERIALNUM_STRING_DESC state
enum_get_serialnum_string_desc_handle
handle function
ENUM_SET_CONFIGURATION state handle
enum_set_configuration_handle
function
enum_dev_configured_handle ENUM_DEV_CONFIGURED state handle function
usbh_enum_desc_get get descriptor in host enumeration phase
usbh_enum_addr_set configure address in host enumeration phase
usbh_enum_cfg_set set configuration in host enumeration phase
usbh_device_desc_parse analysis device descriptor
usbh_cfg_desc_parse analysis device descriptor
usbh_interface_desc_parse analysis interface descriptor
usbh_endpoint_desc_parse analysis endpoint descriptor
usbh_string_desc_parse analysis character string descriptor
usbh_next_desc_get get next descriptor packet header
14
GD32 USBFS/HS Firmware Library User Guide
usbd_status_enum usbd_ctlrx receive data on the control endpoint
usbd_status_enum usbd_ctlstatus_tx transmit status on the control endpoint
usbd_status_enum usbd_ctlstatus_rx receive status on the control endpoint
usbd_ep_stall set an endpoint to STALL status
usbd_ep_clear_stall clear endpoint stalled status
usbd_ep_fifo_flush flushes the fifos
usbd_rxcount_get get the received data length
Application layer include four files, which are shown in Table 1-14. Application interface
layer file table
Main.c file mainly realize main application program interface function, it is shown in Table
1-15. Main.c file function table.
usbh_usr.c file contains user callback function structure, which is shown in Figure 1-7. User
callback function structure.
16
GD32 USBFS/HS Firmware Library User Guide
Figure 1-7. User callback function structure
17
GD32 USBFS/HS Firmware Library User Guide
hwp_time_set hardware timer initialization
USB class include host class function file, USB protocol support serval class, as shown in
Table 1-18. HID/MSC host class library function, HID and MSC host class function file
would be introduced.
USBFS host state machine apply state machine nesting and state machine lookup table
method. USBFS host machine include host machine handle (usbh_core.c), enumeration
state machine handle (usbh_std.c) and control transfer state machine handle (usbh_ctrl.c).
USBFS host state lookup table is shown as Figure 1-8. USBFS host state machine lookup
table, firstly, introduce the methods to lookup table, the table is consist of nine items. Every
item, which contain four parts, is state switch. The first part is current status, the second part
is current received event, the third part is the next status to which will switch, and the fourth
part is event handle function which the status switch need. It means that, in current status, if
receive current corresponding event, through lookup table and execute event function,
switch to next status. It is easily to understand state machine lookup table.
19
GD32 USBFS/HS Firmware Library User Guide
Figure 1-9. USBFS host state machine flow diagram
20
GD32 USBFS/HS Firmware Library User Guide
Figure 1-11. Enumeration state machine flow diagram
21
GD32 USBFS/HS Firmware Library User Guide
Figure 1-12. Control transfer state machine handle lookup table
In the processing of control transfer, initial status is CTRL_IDLE, once control completed, the
status switch to CTRL_COMPLETE, then return upper state machine.
Interrupt of USBD module contain low priority interrupt and high priority interrupt. Commonly,
IN and OUT transaction transfer, which are distinguished by IFR_DIR flag, would been
handled in low priority interrupt. There are two different interrupts for IN and OUT transaction
of USBFS, whose respective flag of interrupt is GINTF_IEPIF and GINTF_OEPIF, which are
shown in Figure 1-13. Control transfer state machine flow diagram. OUT endpoint
interrupt handle function is shown in Figure 1-14. OUT endpoint interrupt handle
function.
22
GD32 USBFS/HS Firmware Library User Guide
Interrupt Flag Description Operation Mode
23
GD32 USBFS/HS Firmware Library User Guide
Figure 1-14. OUT endpoint interrupt handle function
In OUT endpoint interrupt handler function, depending on endpoint interrupt flag register,
interrupt event of OUT endpoint could be distinguished. The events include such events
below: transfer finished event, endpoint disabled event, SETUP phase finished event,
endpoint Rx FIFO overrun event and back-to-back SETUP packets event. When OUT
endpoint event is generated, polling interrupt flag is easy to enter corresponding interrupt
handler function. IN endpoint interrupt handle function is shown in Figure 1-15. IN endpoint
interrupt handle function.
24
GD32 USBFS/HS Firmware Library User Guide
Figure 1-15. IN endpoint interrupt handle function
25
GD32 USBFS/HS Firmware Library User Guide
1.6. USB DEMO
As shown in Figure 1-16. USBFS demo diagram, USB demo compose of host demo and
device demo. Host contain HID host, MSC host, CDC host and IAP host. Device compose of
compsite device and single device. Compsite device could be composed of different device,
like HID and CDC compsite device, MSC and CDC compsite device, CDC and Printer
compsite device and HID and MSC device. According to application protocol, single device
include DFU, HID, Audio, Printer, MSC, CCID and CDC device. There are severval sub HID
class, such as mouse, keyboard, IAP and multi-touch device. MSC device have severval sub
MSC class, like CD-ROM and U-disk device, whose storage medium is MCU flash, SPI flash,
Nand flash, SD card, MCU SRAM and external SRAM. In above those demo, MSC host
demo and HID keyboard demo are selected to introduce Working principle and operating
results of host and device.
Overview
The demo briefly introduce USBFS host as U disk, once U disk is connected to GD32F450i
26
GD32 USBFS/HS Firmware Library User Guide
board, USBFS enumerate the U disk. After U disk completed enumeration, user application
start, the demo could read U disk file index and write file to U disk.
In the demo, there is USBFS host initialization section before while (1), after initialization,
program enter main loop and start USBFS host machine, which would be introduced as
below.
27
GD32 USBFS/HS Firmware Library User Guide
Figure 1-18. USBFS host U disk enumeration information
Firstly, press User key, screen shown U disk information, then press Tamper key, screen
shown root index content. Pressing Wakeup key is writing file to U disk, then user could
observe MSC host demo complete information, which is shown in Figure 1-19. USBFS host
connect U disk routine result.
28
GD32 USBFS/HS Firmware Library User Guide
Figure 1-19. USBFS host connect U disk routine result
Overview
The demo briefly introduce USBFS device, as keyboard, once GD32F450i board is
connected to host, host enumerate the board as keyboard. Press the board key is print the
corresponding character, pressing the tamper key would output ‘a’, pressing the wakeup key
would output ‘b’, pressing the user key would output ‘c’.
USBFS interface module work in device mode, the demo enumerate USBFS device as
keyboard, and its main function is shown in Figure 1-20. USBFS main function
29
GD32 USBFS/HS Firmware Library User Guide
Figure 1-20. USBFS main function
30
GD32 USBFS/HS Firmware Library User Guide
DP pin pull up and disconnect
When DP pin pull-up of USBFS device is automatically realized through hardware detected,
software do not need operation. When VBUS pin detect B class convention is active level,
USBFS interface module could automatically connect pull-up resistance on DP pin, so as to
send full speed device connected signal to host, and trigger device interrupt (SESIF@
USBFS_GINTF).
When MCU detected that VBUS pin voltage level is lower than B class convention valid
voltage level, USBFS module will automatically disconnect, and trigger device interrupt
(SESEND@USBFS_GOTGINTF). Moreover, software is also used to disconnect device.
Configuring software disconnected bit (SD@USBFS_DCTL) of device control register, so as
to enable software disconnect. Then, USBFS interface module remove pull-up resistance on
DP pin, brings out detecting interrupt of device disconnect, thus, even if USB cable is remain
connected, device disconnect interrupt could be identified still.
31
GD32 USBFS/HS Firmware Library User Guide
2. Revision history
32
GD32 USBFS/HS Firmware Library User Guide
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. The Company reserves all rights under such laws and
treaties and does not grant any license under its patents, copyrights, trademarks, or other intellectual property rights. The names and
brands of third party referred thereto (if any) are the property of their respective owner and referred to for identification purposes only.
The Company makes no warranty of any kind, express or implied, with regard to this document or any Product, including, but not
limited to, the implied warranties of merchantability and fitness for a particular purpose. The Company does not assume any liability
arising out of the application or use of any Product described in this document. Any information provided in this document is provided
only for reference purposes. It is the responsibility of the user of this document to properly design, program, and test the functionality
and safety of any application made of this information and any resulting product. Except for customized products which has been
expressly identified in the applicable agreement, the Products are designed, developed, and/or manufactured 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 operation of weapons, weapons systems, nuclear installations, atomic energy
control instruments, combustion control instruments, airplane or spaceship instruments, transportation instruments, traffic signal
instruments, life-support devices or systems, other medical devices or systems (including resuscitation equipment and surgical
implants), pollution control or hazardous substances management, or other uses where the failure of the device or Product could
cause personal injury, death, property or environmental damage ("Unintended Uses"). Customers shall take any and all actions to
ensure using and selling the Products in accordance with the applicable laws and regulations. The Company is not liable, in whole or
in part, and customers shall and hereby do release the Company as well as it’s suppliers and/or distributors from any claim, damage,
or other liability arising from or related to all Unintended Uses of the Products. Customers shall indemnify and hold the Company as
well as it’s suppliers and/or distributors harmless from and against all claims, costs, damages, and other liabilities, including claims for
personal injury or death, arising from or related to any Unintended Uses of the Products.
Information in this document is provided solely in connection with the Products. The Company reserves the right to make changes,
corrections, modifications or improvements to this document and Products and services described herein at any time, without notice.
33