Cypress USBSuite Application Development Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 20

DownloadCypress USBSuite Application Development Guide
Open PDF In BrowserView PDF
Cypress USBSuite

Application Development Guide
Version 1.2.3.20
Doc. No.002-23610 Rev. **

Cypress Semiconductor
198 Champion Court
San Jose, CA 95134-1709
www.cypress.com

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

1

Copyrights
© Cypress Semiconductor Corporation, 2018. This document is the property of Cypress Semiconductor Corporation and its subsidiaries,
including Spansion LLC (“Cypress”). This document, including any software or firmware included or referenced in this document
(“Software”), is owned by Cypress under the intellectual property laws and treaties of the United States and other countries worldwide.
Cypress reserves all rights under such laws and treaties and does not, except as specifically stated in this paragraph, grant any license
under its patents, copyrights, trademarks, or other intellectual property rights. If the Software is not accompanied by a license agreement
and you do not otherwise have a written agreement with Cypress governing the use of the Software, then Cypress hereby grants you a
personal, non-exclusive, nontransferable license (without the right to sublicense) (1) under its copyright rights in the Software (a) for
Software provided in source code form, to modify and reproduce the Software solely for use with Cypress hardware products, only internally
within your organization, and (b) to distribute the Software in binary code form externally to end users (either directly or indirectly through
resellers and distributors), solely for use on Cypress hardware product units, and (2) under those claims of Cypress’s patents that are
infringed by the Software (as provided by Cypress, unmodified) to make, use, distribute, and import the Software solely for use with Cypress
hardware products. Any other use, reproduction, modification, translation, or compilation of the Software is prohibited.
TO THE EXTENT PERMITTED BY APPLICABLE LAW, CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
WITH REGARD TO THIS DOCUMENT OR ANY SOFTWARE OR ACCOMPANYING HARDWARE, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. To the extent permitted by
applicable law, Cypress reserves the right to make changes to this document without further notice. Cypress does not assume any liability
arising out of the application or use of any product or circuit described in this document. Any information provided in this document,
including any sample design information or programming code, 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. Cypress products are not designed, intended, or authorized for use as critical components in systems designed or intended for the
operation of weapons, weapons systems, nuclear installations, 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 system could cause personal injury, death, or property damage (“Unintended Uses”). A critical component is any component
of a device or system whose failure to perform can be reasonably expected to cause the failure of the device or system, or to affect its safety
or effectiveness. Cypress is not liable, in whole or in part, and you shall and hereby do release Cypress from any claim, damage, or other
liability arising from or related to all Unintended Uses of Cypress products. You shall indemnify and hold Cypress 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 Cypress products.
Cypress, the Cypress logo, Spansion, the Spansion logo, and combinations thereof, WICED, PSoC, CapSense, EZ-USB, F-RAM, and
Traveo are trademarks or registered trademarks of Cypress in the United States and other countries. For a more complete list of Cypress
trademarks, visit cypress.com. Other names and brands may be claimed as property of their respective owners.

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

2

Contents

1

Introduction ................................................................................................................................. 4
1.1 Overview .............................................................................................................................. 4
1.2 Cypress USBSuite ............................................................................................................... 6

2

C++ Library CyAPI.lib ................................................................................................................. 8
2.1 Overview .............................................................................................................................. 8
2.2 Writing Your First Application ............................................................................................... 8
2.3 Application Code Analysis.................................................................................................. 13
2.4 Additional Features in the Application ................................................................................ 13

3

C# Library CyUSB.dll ................................................................................................................ 15
3.1 Overview ............................................................................................................................ 15
3.2 Writing Your First Application ............................................................................................. 15
3.3 Application Code Analysis.................................................................................................. 17
3.4 Additional Features in the Application ................................................................................ 18

Revision History ..................................................................................................................................... 20

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

3

1 Introduction

1.1

Overview

Application communication with USB devices has evolved. Earlier, the application writing process was complex and involved
making direct calls to drivers. The application had to first get a device handle and then call device I/O controls or read/write
files.
Cypress released CyAPI.lib, first as part of the USB Developers’ μStudio and then in the Cypress USBSuite. This provided a
high-level programming interface to get a device handle and communicate with Cypress USB devices.
The new generation of application development tools from Cypress has a simpler, more powerful API. This includes a
Cypress USBSuite C++ library, CyAPI.lib, and a Cypress USBSuite C# library, CyUSB.dll.
CyAPI.lib provides a simple, powerful C++ programming interface to communicate with USB devices. More specifically, it is a
C++ class library that provides a high-level programming interface to the CyUsb3.sys kernel mode driver. The library is only
able to communicate with USB devices that are served by (that is, bound to) this driver.
CyUSB.dll is a managed Microsoft .NET class library. It provides a high-level, powerful programming interface to USB
devices. Rather than communicate with USB device drivers directly via Win32 API calls, such as SetupDiXxxx and
DeviceIoControl, applications can access USB devices via library methods such as XferData and properties such as AltIntfc.

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

4

Introduction
Figure 1-1 illustrates the earlier application development environment.
Figure 1-2 offers a comparison of the development environment for applications using the C++ and the C# libraries.

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

5

Introduction

Figure 1-1. Early Days of Application Development

Figure 1-2. C++ vs C#

1.2

Cypress USBSuite

The Cypress USBSuite is a set of development tools for Microsoft Visual Studio to create .NET Windows applications, which
includes the following:

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

6

Introduction

▪
▪

A Generic USB Kernel Mode Driver
A .NET Managed Class Library that has




▪
▪
▪

CyUSB.dll, which is a C# library
CyAPI.lib, which is a C++ library

USB Control Center that serves as a USB experimenter's work-bench
Bulkloop Application that provides a USB bulk transfer test tool
Streamer Application that can be used for transfer performance testing.

The Cypress USBSuite and Cypress USB kernel mode driver (CyUSB3.sys) are WHQL certified and supported on Windows
10 (64 and 32 bit), Windows 8.1 (64 and 32 bit), Windows 8 (64 and 32 bit), Windows 7 (64 and 32 bit), Windows Vista (64
and 32 bit), and Windows XP (32 bit only).

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

7

2 C++ Library CyAPI.lib

2.1

Overview

CyAPI.lib is implemented as a statically linked library. It provides a C++ programming interface to USB devices and enables
users to quickly develop custom USB applications. It enables users to access only devices bound to the CyUsb3.sys driver.
CyAPI.lib takes care of activities such as error handling, which were otherwise handled by the user when making direct calls
to the drivers.
The classes and functions provided by the CyApi.lib can be accessed from C++ applications developed using Microsoft
Visual C++. To use the library, you need to add a reference to CyAPI.lib to your project's Source Files folder and include a
dependency to the header file CyAPI.h. Then, any source file that accesses the CyApi objects must include the CyApi.h
header file.
The following section explains how you can develop your first application with CyAPI.lib. The following examples are written
in Visual C++.

2.2

Writing Your First Application

Before you begin writing your first application, ensure you have installed USBSuite and Microsoft Visual Studio 2008. The
following steps guide you on how to develop your first VC++ application using CyAPI.lib.
1.

Start a new project in Visual Studio 2008 by clicking on File > New > Project.

2.

In the window, select Visual C++ > Windows Forms Application, and give your application a unique name. In this
example, the application name is ‘Example1’ as shown in Figure 2-1.

Figure 2-1. Starting a New Project

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

8

C++ Library CyAPI.lib

3.

Click OK and a blank form is displayed. This form is a functional application. Click the green arrow (Run button) to
start the application. A blank form appears as you start the application.

4.

Right-click on Source Files and select Add > Existing Item, under the Solution Explorer window as shown
inFigure 2-2. Browse to the installation directory of USB Suite and choose the CyAPI.lib according to the system
you are using, and double-click on CyAPI.lib. This references the library to your project. However, you cannot use
it just yet.

Figure 2-2. Adding Reference to CyAPI.lib

5.

If a blank form is displayed, right-click in the white space and click View Code. This is your code view window.
Note that Visual Studio generates some default code to start your project.

6.

At the top, you see many ‘using Namespace’ directives. Include the following lines after the line #pragma:
#include 
#include 
These two headers are required for the primitive datatypes used in CyAPI.h and the USB Plug and Play (PnP)
events respectively.

7.

After adding the reference to CyAPI.lib, expose the interface to it. This can be done by including a line to reference
CyAPI.h, which gives you access to the library’s APIs, classes, and other functionality. This is done in the following
steps:
a)

Go to Project > Properties. In the dialog box, select Configuration Properties > C/C++ > General >
Additional Include Directories. Point it to the inc folder, which contains the CyAPI.h file. Click OK.

b)

Add the following after the lines added in step 6.
#include "CyAPI.h"

c)

Go to Project > Properties. In the dialog box, select Configuration Properties > Linker > Input > Additional
Dependencies and type user32.lib as shown in Figure 2-3.

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

9

C++ Library CyAPI.lib

Figure 2-3. Additional Project Settings

8.

Go to Project > Properties. In the dialog box, select Configuration Properties > General > Common Language
Runtime Support and set it to Common Language Runtime Support (/clr) as shown in Figure 2-4.

Figure 2-4. Project Property Settings

Insert the following code in your application at the exact location in the Form1 class. The code is explained in the

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

10

C++ Library CyAPI.lib
Application Code Analysis section. Note that Form1() and WndProc must be public members.

public ref class Form1 : public System::Windows::Forms::Form
{
public:
CCyUSBDevice *USBDevice, *CyStreamdev;
int AltInterface;
bool bPnP_Arrival;
bool bPnP_Removal;
bool bPnP_DevNodeChange;
Form1(void)
{
InitializeComponent();
USBDevice =new CCyUSBDevice((HANDLE)this->
Handle,CYUSBDRV_GUID,true);
}
virtual void WndProc( Message% m ) override
{
if (m.Msg == WM_DEVICECHANGE)
{
// Tracks DBT_DEVNODES_CHANGED followed by
// DBT_DEVICEREMOVECOMPLETE
if (m.WParam == (IntPtr)DBT_DEVNODES_CHANGED)
{
bPnP_DevNodeChange = true;
bPnP_Removal = false;
}
// Tracks DBT_DEVICEARRIVAL followed by
// DBT_DEVNODES_CHANGED
if (m.WParam == (IntPtr)DBT_DEVICEARRIVAL)
{
bPnP_Arrival = true;
bPnP_DevNodeChange = false;
}
if (m.WParam == (IntPtr)DBT_DEVICEREMOVECOMPLETE)
{
bPnP_Removal = true;
}
// If DBT_DEVICEARRIVAL followed by
// DBT_DEVNODES_CHANGED
if (bPnP_DevNodeChange && bPnP_Removal)
{
bPnP_Removal = false;
bPnP_DevNodeChange = false;
GetDevice();
}

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

11

C++ Library CyAPI.lib

// If DBT_DEVICEARRIVAL followed by
// DBT_DEVNODES_CHANGED
if (bPnP_DevNodeChange && bPnP_Arrival)
{
bPnP_Arrival = false;
bPnP_DevNodeChange = false;
GetDevice();
}
}
Form::WndProc( m );
}
void GetDevice()
{
USBDevice = new CCyUSBDevice((HANDLE)this->
Handle,CYUSBDRV_GUID,true);
AltInterface = 0;
if (USBDevice->DeviceCount() != 0)
{
Text = "Device Attached";
}
else
{
Text = "No Devices Attached";
}
}
}

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

12

C++ Library CyAPI.lib

2.3

Application Code Analysis

Before we analyse the previous code, note that you can find more details about the CyAPI.lib APIs in the API guide
(CyAPI.chm or CyAPI.pdf).
An application normally creates an instance of the CCyUSBDevice class that knows how many USB devices are attached to
the CyUsb3.sys driver. Therefore, a working knowledge of the CCyUSBDevice class is essential. The CCyUSBDevice class
is the primary entry point in the library. All the functionality of the library should be accessed through an instance of
CCyUSBDevice. An instance of CCyUSBDevice is aware of all the USB devices that are attached to the CyUSB3.sys driver
and can selectively communicate with any one of them by using the Open() method. The CCyUSBDevice object created
serves as the programming interface to the driver whose GUID is passed in the guid parameter.
The constructor of this class is as follows:

USBDevice = new CCyUSBDevice((HANDLE)this->Handle, CYUSBDRV_GUID,true);
(HANDLE)this->Handle is a handle to the application's main window (the window whose WndProc function processes USB
PnP events).
Pass CYUSBDRV_GUID as the guid parameter. CYUSBDRV_GUID is a unique constant guid value for the CyUSB3.sys
driver and is specified in the inf file that is used to bind the device to the CyUSB3.sys driver.
These CCyUSBDevice objects are all properly initialized and ready to use.
MainForm's WndProc method is used to watch for PnP messages. Windows sends all top-level windows a set of default
messages when new devices or media are added and become available, and when existing devices or media are removed.
These messages are known as WM_DEVICECHANGE messages. Each of these messages has an associated event, which
describes the change.
When a device is added, or removed from the system, the system broadcasts the DBT_DEVNODES_CHANGED device
event using the WM_DEVICECHANGE message. The operating system sends the DBT_DEVICEARRIVAL device message
when a device is inserted and becomes available. Similarly, a _DEVICEREMOVAL device message is sent when a device is
removed.
The WndProc takes the message sent by the operating system as an argument and if the message indicates a device
attached or a device removed status, calls the GetDevice() function to update the status of USB devices connected to the
host bound to the CyUSB3.sys driver.
The GetDevice() function uses the DeviceCount() function, which is a member of the CCyUSBDevice class. The
DeviceCount() function returns the number of devices attached to the CyUSB3.sys driver. If this function returns a non-zero
value, it means that there are one or more devices connected to the host that are bound to the CyUSB3.sys driver.
These lines of code display the status of device connection to host. If there are one or more devices connected to host, the
“Device Attached” text is displayed. If no devices are attached, the “No Devices Attached” text is displayed. The Text
property controls the text seen at the top left corner when you run your application. For example, if you run the application
without the code, the word Form1 is displayed, which is not very informative. Adding this code every time a device is plugged
in or removed updates the text.
Press the green Play button and attach and detach a USB device.
Make sure it is a Cypress USB device, because the event handler you write only handles devices tied to the CyUSB driver.
Unplug and plug the device repeatedly and watch the displayed text change.
These are the basics of writing your own application. The next few sections discuss features that make the application more
productive.

2.4

Additional Features in the Application

The CCyUSBDevice provides the following two components (a detailed list is located in the CyAPI Programmers Reference
Guide):

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

13

C++ Library CyAPI.lib
1.

Functions

2.

Properties (Data Members)

These two components give you access to most of the USB controls needed for your application, including functions such as
GetDeviceDescriptor(), Reset(), and SetAltIntfc(); properties such as DeviceName, DevClass, VendorID (VID), and
ProductID (PID).

2.4.1

Detecting Devices

The first application you wrote allowed you to detect PnP events and change the text of the application. This section explains
how you can create a Listview that displays the currently connected USB devices. The following code generates an
application that detects all devices connected to the bus.

▪
▪
▪
▪

Click Form1.h [Design] tab in Visual Studio.
Click View > Toolbox.
Drag and drop listBox in the form and expand it to take up most of the room on the form.
Right-click in the white space outside of your form and click View Code.

Insert the following code to Form1.
void RefreshList()
{
listBox1->Items->Clear();
listBox1->Text = " ";
for(int i=0; iDeviceCount(); i++)
{
USBDevice->Open(i);
listBox1->Items->Add(gcnew String(USBDevice->
FriendlyName));
listBox1->Text= Convert::ToString(
listBox1->Items[i]);
}
}
This function connects all the devices to the CyUSB3.sys driver and displays their names in a listbox. The DeviceCount()
function is implemented in CyAPI.lib and returns the number of USB devices attached to the host, which are bound to
CyUSB3.sys driver. This function should be called every time a device is attached or removed to update the list of devices.
This single function fills the listBox with the friendly names of all the USB devices bound to the CyUSB3.sys driver.
listBox1 ItemsClear(); – It clears the tree every time the function is called. The open() function gives a handle to the
USBdevice attached to the CyUSB3.sys driver and the FriendlyName property contains the device description string for the
open device, which was provided by the driver's .inf file.
Call the RefreshList() function from the GetDevice() function.
This code calls the above function in GetDevice() and inside Form() constructor. When the application starts, the Listbox is
populated with the initial list of devices attached. When a USB PnP event occurs (attach/detatch), the listbox gets populated
with a fresh list of currently connected USB devices.

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

14

3 C# Library CyUSB.dll

3.1

Overview

The USBSuite.net framework enables users to quickly develop custom USB applications. At the heart of this framework is
the cyusb.dll. This DLL is a managed Microsoft .NET class library. It provides a high level, powerful programming interface to
USB devices. Instead of communicating with USB device drivers directly through Win32 API calls (such as SetupDiXxxx and
DeviceIoControl), applications can access USB devices through library methods such as XferData and properties such as
AltIntfc.
Because cyusb.dll is a managed .NET library, its classes and methods can be accessed from any of the Microsoft Visual
Stuido.NET managed languages such as Visual Basic.NET, C#, Visual J#, and Managed C++. To use the library, you must
add a reference to cyusb.dll to your project's References folder. Then, any source file that accesses the CyUSB name space
must include a line to add the name space in the appropriate syntax.

3.2

Writing Your First Application

Before you begin writing your first application, ensure you have installed USBSuite and Visual Studio 2008. The following
steps guide you to develop your first VC# application using CyUSB.dll.
1.

Start Visual Studio 2008 and choose File > New Project > Windows Form Application.

In the window that pops up, make sure you give your application a unique name. In this example, the application name is
‘WindowsFormApplication1’ as shown in Figure 3-1.
Figure 3-1. First Application - WindowsFormApplication1

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

15

C# Library CyUSB.dll

2.

Click OK and a blank form is displayed. This form is a functional application. Click on the green arrow (Run button) to
start the application. The blank form appears as you start the application.

3.

There are many interesting things you can do with CyUSB.dll. To use this library, you must add a reference to
CyUSB.dll to your project's References folder. Any source file that accesses the CyUSB name space must include a line
to include the name space in the appropriate syntax.

4.

For that, right-click on Reference and Add Reference, under the Solution Explorer window as in Figure 3-2. Select the
Browse tab from the window that pops up and browse to the installation directory of USBSuite and double-click
CyUSB.dll. This references the library to your project. However, you cannot use it just yet.

Figure 3-2. Adding reference to CyUSB.dll

5.

If you see the blank form, right click outside in the white space and click View Code. This is your code view window.
Note that Visual Studio starts off the file with some default initialization code.

6.

At the top of the file, there are directives starting with the word ‘Using’. For C/C++ users, these are the same as
‘#include’. Since you added the reference to CyUSB.dll, you must inform the application that it is going to be used.
Under the last ‘using’ line, add the words: using CyUSB; this gives you access to the library’s APIs, classes, and other
functionality.

7.

The ‘using’ directive actually serves as a shortcut in the code so that you do not have to explicitly reference the CyUSB
name space when you use its functions. You can leave out the ‘using’ directive if you affix ‘CyUSB’ to all the CyUSB
member references.

8.

Insert the following code inside the Form1 class definition (refer to Application Code Analysis).

USBDeviceList usbDevices;
CyUSBDevice myDevice;
public Form1()
{
InitializeComponent();

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

16

C# Library CyUSB.dll

usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
usbDevices.DeviceAttached += new EventHandler
(usbDevices_DeviceAttached);
usbDevices.DeviceRemoved += new EventHandler
(usbDevices_DeviceRemoved);
// Get the first device having VendorID == 0x04B4
// and ProductID == 0x00F3
myDevice = usbDevices[0x04B4, 0x00F3] as CyUSBDevice;
}
void usbDevices_DeviceAttached(object sender, EventArgs e)
{
//Add code to handle Device arrival
}
void usbDevices_DeviceRemoved(object sender, EventArgs e)
{
//Add code to handle Device removal
}

3.3

Application Code Analysis

Before we analyse the previous code, note that you can find more details about the CyUSB.dll APIs in the API guide
(CyUSB.NET.chm or CyUSB.NET.pdf).
An application normally creates an instance of the USBDeviceList class, which represents a list of USB devices. Thus a
good working knowledge of the USBDeviceList class is essential. The USBDeviceList represents a dynamic list of USB
devices that are accessible through the class library. When an instance of USBDeviceList is created, it populates itself with
USBDevice objects representing all the USB devices served by the indicated device selector mask, such as (line 6) in the
previous code snippet:

usbDevices = new USBDeviceList (CyConst.DEVICES_CYUSB);
The CyConst.DEVICES_CYUSB is a type mask that selects devices that are bound to the CyUsb3.sys driver.
After an instance of the USBDeviceList class is constructed, the USBDeviceList index operators make it easy to locate a
particular device and begin using it, as shown below:

CyUSBDevice myDevice = usbDevices[0x04B4, 0x00F3] as CyUSBDevice;
If you want to search for other methods of indexing through the device list, type: CyUSBDevice myDev = usbDevices[.
After you type in the open bracket, Visual Studio displays different methods of using the USBDeviceList index. Because
USBDeviceList implements the IEnumerable interface, you can iterate through a USBDeviceList object's items using the
‘foreach’ keyword.
Windows PlugNPlay (PnP) event handling for the devices in a USBDeviceList are also supported by the library. To enable
handling of PnP events,USBDeviceList provides two event handlers DeviceAttached and DeviceRemoved.
DeviceAttached - When a new USB device is plugged into the bus, the arrival of a new USB device is detected by this event.
Handling of the event requires that an EventHandler object be assigned to the DeviceAttached event handler.

usbDevices.DeviceAttached += new EventHandler
(usbDevices_DeviceAttached);
The previous line of code assigns usbDevices_DeviceAttached as the event handler for the DeviceAttached Event. Any
action to be taken on arrival of the USB device should be done within the following event handler function

void usbDevices_DeviceAttached(object sender, EventArgs e)

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

17

C# Library CyUSB.dll

DeviceRemoved - When a USB device is disconnected from the bus, the removal of the USB device is detected by this
event. Handling of the event requires that an EventHandler object be assigned to the DeviceRemoved event handler.

usbDevices.DeviceRemoved += new
EventHandler(usbDevices_DeviceRemoved);
The above line of code assigns usbDevices_ DeviceRemoved as the event handler for the DeviceRemoved Event. Any
action to be taken on removal of the USB device should be done within the following event handler function:

void usbDevices_DeviceRemoved(object sender, EventArgs e)
You can add some code to update the display in both the Event Handlers, and then test the code.
1.

Inside the usbDevices_DeviceRemoved event handler, type the following:Text = “Device Removed”;

2.

Inside the usbDevices_DeviceAttached event handler, type the following: Text = “Device Attached”;

The ‘Text’ property controls the text seen in the top left hand corner when you run your application. By adding this code
every time a device is plugged in or removed, the software displays the text provided.
1.

Press the green Play button and attach and detach a Cypress USB device.

2.

Unplug and plug the device repeatedly and watch the text change. That is your first application.

The next few sections discuss features used to make the application more productive.

3.4

Additional Features in the Application

Before proceeding to the next topic, a more detailed discussion about CyUSB.dll is required. One of the most important
classes in the library is CyUSBDevice. The CyUSBDevice class represents a USB device attached to the CyUSB3.sys
device driver. A list of CyUSBDevice objects are generated by passing DEVICES_CYUSB mask to the USBDeviceList
constructor. After you obtain a CyUSBDevice object, you can communicate with the device through the objects’ various
endpoint (ControlEndPt, BulkInEndPt, BulkOutEndPt, and others) members. Because CyUSBDevice is a descendant of
USBDevice, it inherits all the members of USBDevice.
CyUSBDevice provides three main components (a detailedist is located in the USBSuite Programmers Reference Guide).

▪
▪
▪

Functions (in C# parlance, these are called methods)
Properties
Objects

These three components give you access to most of the USB controls you need in your application, including functions such
as GetDeviceDescriptor() and Reset(); properties such as AltIntfc and ConfigCount; and objects such as BulkIn/Out Endpt
and IsocEndpt.

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

18

C# Library CyUSB.dll

3.4.1

Detecting Devices

This section explains how to create a tree view that displays the currently connected USB devices. The following code
generates an application that detects all devices connected to the bus.
3.

Move the buttons to one side of your form.

4.

Drag and drop TreeView onto the form and expand it to take up most of the room on the form.

5.

Right click in the white area outside of your form and click View Code.

6.

Add the following code to the function: Form1( )

foreach (USBDevice dev in usbDevices)
treeView1.Nodes.Add(dev.Tree);
Adding these two lines of code allows the program to get all devices connected to the host and fill in the tree when the
application starts. Every USB device has a Tree property associated with it. When you call dev.Tree, the configuration of all
the devices is returned and displayed.
7.

Add the following code to both the PnP event handlers. Before doing this, you have to add one more line to ensure
repeat devices do not show up in the tree, that is, to clear the treeview first and then populate it with the fresh list of
devices. Add the following code under both usbDevices_DeviceAttached and usbDevices_DeviceRemoved
event handlers:

treeView1.Nodes.Clear();
foreach (USBDevice dev in usbDevices)
treeView1.Nodes.Add(dev.Tree);
This clears the tree every time a device is plugged or unplugged and then it re-initializes the tree. Your view should look
similar to the following figure .
Figure 3-3. CYUSB Template

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

19

Revision History

Document Revision History
Document Title: Cypress USBSuite Application Development Guide
Document Number: 002-23610
Revision

Issue Date

Origin of

Description of Change

Change

**

04/12/2018

MKRS

New application development guide.

Cypress USBSuite Application Development Guide

Doc. No. 002-23610 Rev. **

20



Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.7
Linearized                      : No
Page Count                      : 20
Language                        : en-US
Tagged PDF                      : Yes
XMP Toolkit                     : 3.1-701
Producer                        : Microsoft® Word 2016
Creator                         : namjtmp2
Description                     : 002-23610 Rev. **
Creator Tool                    : Microsoft® Word 2016
Create Date                     : 2018:05:08 14:17:16+08:00
Modify Date                     : 2018:05:08 14:17:16+08:00
Document ID                     : uuid:094C705B-8FBB-4BE6-BFA5-002333D0FE78
Instance ID                     : uuid:094C705B-8FBB-4BE6-BFA5-002333D0FE78
Author                          : namjtmp2
Subject                         : 002-23610 Rev. **
EXIF Metadata provided by EXIF.tools

Navigation menu