UG1004E
UG1004E
UG1004-1.0E, 6/29/2021
Copyright © 2021 Guangdong Gowin Semiconductor Corporation. All Rights Reserved.
Contents
Contents ............................................................................................................... i
3 libusb_WinUSB Programming........................................................................ 5
3.1 Libusb Initialization and Exit ............................................................................................... 5
6 Error Code...................................................................................................... 17
UG1004-1.0E i
List of Figures
List of Figures
Figure 2-2 Select the Device that Requires Driver Installation ....................................................... 3
UG1004-1.0E ii
List of Tables
List of Tables
UG1004-1.0E iii
1 General Description
1 General Description
UG1004-1.0E 1(20)
2 Driver Installation and Uninstallation 2.1 Use Zadig to Install Driver
UG1004-1.0E 2(20)
2 Driver Installation and Uninstallation 2.1 Use Zadig to Install Driver
Click “Install Driver”. The driver will be installed after a few moments.
Note!
The button displays "Install Driver" if the driver is not currently installed, and "Replace
Driver" if another driver is currently installed.
UG1004-1.0E 3(20)
2 Driver Installation and Uninstallation 2.2 Uninstall Driver
In the pop-up dialog box, first check "Remove driver software for this
device", and then click the “Uninstall” button to uninstall the driver.
Figure 2-5 Uninstall Device
UG1004-1.0E 4(20)
3 libusb_WinUSB Programming 3.1 Libusb Initialization and Exit
3 libusb_WinUSB Programming
if (rc < 0)
return rc;
UG1004-1.0E 5(20)
3 libusb_WinUSB Programming 3.2 Open the Specified USB Device
libusb_exit(NULL);
libusb_context * ctx,
uint16_t vendor_id,
uint16_t product_id
);
The parameter ctx is the address of the context struct generated when
initializing libusb. If the default context is used, use NULL. vendor_id and
product_id are the VID and PID of the USB device, respectively. The VID of
Gowin USB device is 0x33aa, and the PID of the U2X_SPI device is
0x0020.
The return value is the pointer to the operation handle of the first
matching device found by libusb on this computer, otherwise it returns the
null pointer NULL.
Examples of use are as follows:
devh = libusb_open_device_with_vid_pid(NULL, 0x33aa, 0x0020);
if(NULL == devh) {
goto out;
libusb_context * ctx,
UG1004-1.0E 6(20)
3 libusb_WinUSB Programming 3.2 Open the Specified USB Device
The parameter ctx is the address of the context struct generated when
initializing libusb. If the default context is used, use NULL. “list” is the pointer
to storage device list.
At the end of use, the memory should be freed using the
libusb_free_device_list() function.
If the function is executed correctly, the return value is the number of
devices and the list saves the list of found devices. Otherwise, a libusb_error
value less than zero is returned.
int libusb_open (
libusb_device *dev,
libusb_device_handle **dev_handle
The parameter dev is the device in the device list, and dev_handle is
the address that saves the pointer of the returned device handle.
If the device is opened successfully, the return value is zero, otherwise
an libusb_error value less than zero is returned.
Examples of use are as follows:
cnt = libusb_get_device_list(NULL, &devs);
if(cnt < 0) {
return -1;
libusb_open(dev[i], dev_handle);
break;
} else {
//the current device is not wanted, close it and check the next one.
libusb_close(dev_handle);
UG1004-1.0E 7(20)
3 libusb_WinUSB Programming 3.3 Interface Declaration
libusb_device_handle * dev_handle,
int interface_number
if (rc < 0) {
goto out;
UG1004-1.0E 8(20)
4 Parameter Configuration
4 Parameter Configuration
data_shift_direction DataShftDir;
sclk_polarity ClkPol;
sclk_phase ClkPha;
sel_polarity SelPol;
} u2x_spi_config;
UG1004-1.0E 9(20)
4 Parameter Configuration
MSB_FIRST,
LSB_FIRST
} data_shift_direction;
CPOL_0,
CPOL_1
} sclk_polarity;
CPHA_0,
CPHA_1
} sclk_phase;
SEL_POL_LO,
SEL_POL_HI
} sel_polarity;
UG1004-1.0E 10(20)
5 U2X_SPI API Functions 5.1 Parameter Configuration
libusb_device_handle *devh,
u2x_spi_config *pSpiConfig,
);
Parameters:
devh: device handle of libusb;
pSpiConfig: A pointor to the “u2x_spi_config” struct, use this struct to
configure parameters for U2X_SPI device.
uiTimeout: timeout parameter, in milliseconds;
Return Value:
Returns 0 if the function runs successful, otherwise an error code less
than zero is returned.
libusb_device_handle *devh,
UG1004-1.0E 11(20)
5 U2X_SPI API Functions 5.2 Send / Receive Byte Data
u2x_spi_config *pSpiConfig,
);
Parameters:
devh: device handle of libusb;
uiDataByteCnt: The count of bytes to control the data transmitting
and receiving.
pucWriteData: A pointer to that holds the data to be transmitted. If no
data needs to be sent, set this parameter to NULL.
pucReadData: A pointer to that holds the received data. If no data
needs to be received, set this parameter to NULL.
uiAddr: Address segment data. If the length of the address segment
configuration is non-zero, the address data will be sent. The maximum
length of the address segment is 4 bytes.
ucCmd: Command segment data. If it’s enabled, the byte data will
be sent in the command segment.
uiDummyCnt: DummySCLK, that is, the number of waiting clock
cycles between the command segment, address segment, and data
segment. If this parameter is non-zero, send the SCLK with the specified
number of cycles after sending the command segment and address
segment, and then send the data segment. The maximum value of this
parameter is 65536. If DummySCLK segment is not needed, set this
parameter to 0.
pSpiConfig: A pointor to the “u2x_spi_config” struct, use this struct to
control the reading and writing of U2X_SPI device.
uiTimeout: timeout parameter, in milliseconds;
Return Value:
Returns 0 if the function runs successful, otherwise an error code less
UG1004-1.0E 12(20)
5.3 Send / Receive Multiple Bits of
5 U2X_SPI API Functions
Data
libusb_device_handle *devh,
u2x_spi_config *pSpiConfig,
);
Parameters:
devh: device handle of libusb;
uiDataBitCnt: The count of bits to control the data transmitting and
receiving.
pucWriteData: A pointer to that holds the data to be transmitted. If no
data needs to be sent, set this parameter to NULL.
pucReadData: A pointer to that holds the received data. If no data
needs to be received, set this parameter to NULL.
uiAddr: Address segment data. If the length of the address segment
configuration is non-zero, the address data will be sent. The maximum
length of the address segment is 4 bytes.
ucCmd: Command segment data. If it’s enabled, the byte data will
be sent in the command segment.
uiDummyCnt: DummySCLK, that is, the number of waiting clock
cycles between the command segment, address segment, and data
segment. If this parameter is non-zero, send the SCLK with the specified
UG1004-1.0E 13(20)
5 U2X_SPI API Functions 5.4 Programming Example
// Main Function
int rc = 0;
rc = libusb_init(NULL);
if (rc < 0)
return rc;
if(NULL == devh) {
goto out;
UG1004-1.0E 14(20)
5 U2X_SPI API Functions 5.4 Programming Example
rc = libusb_claim_interface(devh, 0);
if (rc < 0) {
goto out;
ucWrData[i] = i;
pSpiConfig->DataShftDir = MSB_FIRST;
pSpiConfig->ClkPol = CPOL_0;
pSpiConfig->ClkPha = CPHA_0;
pSpiConfig->SelPol = SEL_POL_LO;
u2x_spi_read_write_bytes(devh,
8, // Send eight cycles of Dummy SCLK after command segment and adress
segment
UG1004-1.0E 15(20)
5 U2X_SPI API Functions 5.4 Programming Example
u2x_spi_read_write_bits(devh,
8, // Send eight cycles of Dummy SCLK after command segment and adress
segment
out:
if(devh)
libusb_close(devh);
libusb_exit(NULL);
return 0;
UG1004-1.0E 16(20)
6 Error Code
6 Error Code
Value Enumerator
0 SUCCESS Runs correctly
-1 USB_ERROR_IO USB input/output error
-2 USB_ERROR_INVALID_PARAM USB parameter error
-3 USB_ERROR_ACCESS No permission to access the device
-4 USB_ERROR_NO_DEVICE No USB device (device
disconnected)
-5 USB_ERROR_NOT_FOUND No Entity
-6 USB_ERROR_BUSY USB device busy
-7 USB_ERROR_TIMEOUT Timeout
-8 USB_ERROR_OVERFLOW Memory overflow
-9 USB_ERROR_PIPE Pipe error
-10 USB_ERROR_INTERRUPTED The system function was interrupted
-11 USB_ERROR_NO_MEM Out of memory
-12 USB_ERROR_NOT_SUPPORTED Not supported on the current
platform
-13 U2X_SPI_ERROR_USBTRANS_ERR USB data transmitting error
-14 U2X_SPI_ERROR_INVALID_PARAM Invalid parameter setting
-15 U2X_SPI_ERROR_TIMEOUT U2X_SPI transmitting timeout
UG1004-1.0E 17(20)
6 Error Code
Value Enumerator
-16 U2X_SPI_ERROR_CMD_ERR U2X_SPI command error
-99 ERROR_OTHER Other errors
UG1004-1.0E 18(20)
Terminology and Abbreviations
UG1004-1.0E 19(20)
Support and Feedback
UG1004-1.0E 20(20)