MSP430 USB HID Windows API Programmers Guide
MSP430 USB HID Windows API Programmers Guide
Introduction
The USB Human Interface Device (HID) class is one of the most widely-supported device classes among
operating systems. Although originally developed for mice and keyboards, it has many advantages in
general-purpose usage.
A common choice for general-purpose USB applications is the virtual COM port, which can be
implemented using the Communications Device Class (CDC). COM ports are easy to implement on host
platforms, and they are well understood by developers. A downside is that USB virtual COM port solutions
for Windows machines require the end user to go through a device installation process. In addition to
being a minor hassle, this process can go wrong if the user chooses the wrong options. Further, some
users in corporate environments do not have administrative rights, so they are not able to do device
installations without the help of a network administrator. In contrast, an HID device loads silently,
bypassing all these issues.
HID does have a few disadvantages. Compared to virtual COM ports, many software engineers are
unfamiliar with its use. HID relies on "reports" to carry the data, and often these complex formats provide
no real value to a general-purpose application. The bandwidth on basic HID implementations is limited to
64KB/sec.
To make up for these disadvantages, TI provides a Windows HID API and demo project. It is streamlined
for use with the datapipe interface that can be implemented by the MSP430 HID API stack, which
eliminates the need to create HID reports. It works with composite HID devices, allowing multiple HID
interfaces to easily be put in composite within the USB device, multiplying the bandwidth.
Used together, the MSP430 HID API stack and Windows HID API form an end-to-end solution that is more
advantageous than virtual COM ports for many general-purpose applications.
For the USB support software for MSP430 MCUs, see the MSP430 USB Developers Package at
http://focus.ti.com/docs/toolsw/folders/print/msp430usbdevpack.html.
Implementation
www.ti.com
Implementation
This Windows MSP430 HID API is hereafter referred to as the Windows API.
2.1
Overview
Figure 1 shows the position of this API within a Windows software stack and in relation to the USB HID
API on the MSP430 device.
Application
User mode
Kernel mode
User
Application
MSP430
HID API
HID Driver
USB Driver
Hardware
USB
PC
Hardware
MSP430
Implementation
www.ti.com
2.2
File Organization
Table 1 shows the files in the API and demo application.
Table 1. Source Code Files
File
Description
Demo Application
*.cpp
2.3
hiddevice.c
Code implementations of the API functions calls and supporting function calls.
hiddevice.h
Definitions for the API function calls that can be used by the application.
System Requirements
The Windows API, as distributed, is written for Visual Studio 2008, using the Microsoft Foundation Class
(MFC) library. As such, it is usable on a wide variety of Windows platforms, including all flavors of
Windows Vista/XP/7. The API is written in C, and the demo application is written in C++. The Express
version of Visual Studio is sufficient for the compiling the API. However, the full version is required to
compile the demo application.
The API also requires the Windows Driver Kit (WDK) to be installed. This is available from Microsoft at no
cost. Version 7 is recommended, because it results in speed gains relative to previous versions.
To
1.
2.
3.
4.
2.4
Implementation
2.5
www.ti.com
How Windows Maps Physical USB HID Devices to the Host Application
Every USB device contains a vendor ID (VID) and a product ID (PID). USB hosts use the VID/PID
combination to identify product types. It stands to reason that all products of a given PID from a vendor of
a given VID are functionally identical. The host associates all devices with that VID/PID with a particular
USB device class for example, HID or CDC. Whenever it encounters this VID/PID, it loads that particular
driver.
Above the Windows HID driver, things look slightly different. The HID driver maintains a list of "HID
devices" on the bus, selectable by a generic index, and makes this list available to the host application
above it. The "HID devices" in this list are logical entities, not physical USB devices. These logical entities
don't necessarily map directly to the physical devices on the bus.
As an example, it is possible that more than one "HID device" with a given VID/PID may be reported to the
host application. This could mean there are multiple physical devices of a given product type attached to
this host; or it might be that there is a composite device that has multiple HID interfaces (which can be
thought of as logical devices). Further, if there are 4+ "HID devices", it is possible that both these factors
are involved, as in Figure 2.
VID: 0x4321
PID: 0xF123
Sernum: ABCD
Type: Composite HID+HID
Windows HID Device List
HID Interface #0
HID Device #0
HID Interface #1
VID: 0x4321
PID: 0xF123
Sernum: ABCE
Type: Composite HID+HID
HID Device #1
HID Device #2
HID Device #3
HID Interface #0
HID Interface #1
Implementation
www.ti.com
Once the host application has the VID, PID, and serial number of every HID device it sees on the bus, it
can begin to understand the situation on the bus. If it finds two HID devices with a given VID/PID, with the
serial numbers also the same, then it can know this is a composite HID device that has two HID
interfaces. If, instead, these two HID devices have different serial numbers, then the application is looking
at two physical single-interface HID devices.
With these techniques, the developer can then design devices with multiple HID interfaces, using each
one as a datapipe for a particular kind of information the equivalent of multiple COM ports. Among other
benefits, this helps overcome the bandwidth limitation of a single HID interface (64 kbps). As many as
eight HID interfaces can be run in parallel on an MSP430, providing 512-kbps bandwidth.
It should be added that if Windows recognizes in the USB descriptors that this device is a mouse or
keyboard during initial enumeration, then Windows itself becomes the "host application" for this HID
device. It issues report requests and uses the data to control the mouse on the screen or input data for
text entry.
2.6
2.7
Sending/Receiving Data
Once the HID interface is opened and assuming this interface is set up as a "datapipe" interface on the
MSP430 HID API data chunks of any size can be sent/received with the API using the HID_writeFile()
and HID_readFile() calls. The calls automatically handle packetization into one or more HID reports.
If implementing a "traditional" HID device rather than "datapipe" meaning the report format has been
customized and the HID-traditional function calls are being used in the MSP430 HID application then it is
still possible to use this API as an example, but it will be necessary to customize the API code.
Implementation
2.8
www.ti.com
This function needs to determine if any of the "HID devices" used by this application were removed from
the system. To that end, it can call HID_IsDeviceAffected() for each open HID device to determine if they
have been removed.
When exiting the application, the application should be sure to unregister it using
HID_UnregisterForDeviceNotification().
www.ti.com
3.1
3.1.1
Description
VOID HID_Init()
Initializes the HID device data structure used to store information about the HID
device/interface.
DWORD HID_GetNumOfInterfaces()
Returns the number of HID devices on the system associated with a particular VID/PID
and serial number.
BYTE HID_Open()
BYTE HID_Close()
BYTE HID_GetSerNums()
3.1.2
WORD pid
struct strTrackSerialNumbers
*serialNumList
The structure to contain the list of serial numbers associated with vid/pid.
returns
The function populates the structure with the serial numbers found.
non-0: The number of physical USB devices on the system with this VID/PID.
Description
DWORD deviceNum
char serialNum[SERNUM_LEN];
3.1.3
www.ti.com
UINT pid
DWORD numSerNums
returns
3.1.4
WORD vid
WORD pid
DWORD deviceIndex
The index within the available devices of this VID/PID and serial number.
char serialNumber[SERNUM_LEN]
Char string of size SERNUM_LEN (40) of the serial number of the device to be found.
DWORD totalDevNum
The total number of HID interfaces of a the physical device described by this VID/PID and
serial number.
DWORD totalSerNum
The total number of available physical USB devices for this VID/PID
returns
www.ti.com
3.1.5
returns
3.1.6
USHORT* VersionNumber
returns
3.2
Sending/Receiving Data
The calls shown in Table 10 pertain to sending/receiving data.
Table 10. Device Connection Management Call Summary
Function
Description
BYTE HID_WriteFile()
BYTE HID_ReadFile()
3.2.1
www.ti.com
BYTE* buffer
DWORD bufferSize
DWORD bytesSent
returns
3.2.2
BYTE* buffer
DWORD bufferSize
DWORD* bytesReturned
returns
10
www.ti.com
3.3
3.3.1
Description
BYTE HID_RegisterForDeviceNotification()
BYTE HID_UnregisterForDeviceNotification()
BYTE HID_IsDeviceAffected()
HDEVNOTIFY* diNotifyHandle
Returns
3.3.2
3.3.3
HDEVNOTIFY* diNotifyHandle
Returns
HID_DEVICE_SUCCESS
Returns
TRUE or FALSE
11
Demo Application
www.ti.com
Demo Application
A simple demo application is provided which shows how to use the API. The application can be thought of
as similar to a COM port terminal application, like Hyperterminal. It sends and receives chunks of data in a
similar fashion, except it does over an HID interface.
12
www.ti.com
Once the connection is initialized, data can be sent to the device by entering text and pressing "Send".
Data received by the application from the device at any time is displayed in the large text field. The
receive window can be cleared with the "Clear" button.
If the HID device is removed from the bus while another device is being accessed, or if another HID
device of this VID/PID is added, the application automatically updates the pulldown menu list.
References
1. MSP430 USB API Stack Programmer's Guide (accompanies the API stack download; see the link to
the MSP430 USB Developer's Package, located at http://www.ti.com/430usb)
2. Microsoft Developers Network reference for HID:
http://msdn.microsoft.com/en-us/library/dd446410.aspx
3. If support is needed, go to http://www.ti.com/msp430 and see the support options.
13
www.ti.com
14
Description
Data Origin
HANDLE hndHidDevice
BOOL bDeviceOpen
UINT uGetReportTimeout
Timeout value for reading data, in milliseconds. This is Must be initialized by the
the amount of time HID_ReadFile() will wait for any
application. The demo application
given report from the device within a data read
does this in UsbAppDlg.cpp.
operation.
UINT uSetReportTimeout
OVERLAPPED oRead
Initialized by HID_Init()
OVERLAPPED oWrite
Initialized by HID_Init()
WORD wInReportBufferLength
WORD wOutReportBufferLength
BYTE inBuffer[256]
WORD inBufferUsed
www.ti.com
Size
Report ID
1 byte (0x3F)
Size
1 byte
Data
15
IMPORTANT NOTICE
Texas Instruments Incorporated and its subsidiaries (TI) reserve the right to make corrections, modifications, enhancements, improvements,
and other changes to its products and services at any time and to discontinue any product or service without notice. Customers should
obtain the latest relevant information before placing orders and should verify that such information is current and complete. All products are
sold subject to TIs terms and conditions of sale supplied at the time of order acknowledgment.
TI warrants performance of its hardware products to the specifications applicable at the time of sale in accordance with TIs standard
warranty. Testing and other quality control techniques are used to the extent TI deems necessary to support this warranty. Except where
mandated by government requirements, testing of all parameters of each product is not necessarily performed.
TI assumes no liability for applications assistance or customer product design. Customers are responsible for their products and
applications using TI components. To minimize the risks associated with customer products and applications, customers should provide
adequate design and operating safeguards.
TI does not warrant or represent that any license, either express or implied, is granted under any TI patent right, copyright, mask work right,
or other TI intellectual property right relating to any combination, machine, or process in which TI products or services are used. Information
published by TI regarding third-party products or services does not constitute a license from TI to use such products or services or a
warranty or endorsement thereof. Use of such information may require a license from a third party under the patents or other intellectual
property of the third party, or a license from TI under the patents or other intellectual property of TI.
Reproduction of TI information in TI data books or data sheets is permissible only if reproduction is without alteration and is accompanied
by all associated warranties, conditions, limitations, and notices. Reproduction of this information with alteration is an unfair and deceptive
business practice. TI is not responsible or liable for such altered documentation. Information of third parties may be subject to additional
restrictions.
Resale of TI products or services with statements different from or beyond the parameters stated by TI for that product or service voids all
express and any implied warranties for the associated TI product or service and is an unfair and deceptive business practice. TI is not
responsible or liable for any such statements.
TI products are not authorized for use in safety-critical applications (such as life support) where a failure of the TI product would reasonably
be expected to cause severe personal injury or death, unless officers of the parties have executed an agreement specifically governing
such use. Buyers represent that they have all necessary expertise in the safety and regulatory ramifications of their applications, and
acknowledge and agree that they are solely responsible for all legal, regulatory and safety-related requirements concerning their products
and any use of TI products in such safety-critical applications, notwithstanding any applications-related information or support that may be
provided by TI. Further, Buyers must fully indemnify TI and its representatives against any damages arising out of the use of TI products in
such safety-critical applications.
TI products are neither designed nor intended for use in military/aerospace applications or environments unless the TI products are
specifically designated by TI as military-grade or "enhanced plastic." Only products designated by TI as military-grade meet military
specifications. Buyers acknowledge and agree that any such use of TI products which TI has not designated as military-grade is solely at
the Buyer's risk, and that they are solely responsible for compliance with all legal and regulatory requirements in connection with such use.
TI products are neither designed nor intended for use in automotive applications or environments unless the specific TI products are
designated by TI as compliant with ISO/TS 16949 requirements. Buyers acknowledge and agree that, if they use any non-designated
products in automotive applications, TI will not be responsible for any failure to meet such requirements.
Following are URLs where you can obtain information on other Texas Instruments products and application solutions:
Products
Applications
Audio
www.ti.com/audio
Amplifiers
amplifier.ti.com
www.ti.com/computers
Data Converters
dataconverter.ti.com
Consumer Electronics
www.ti.com/consumer-apps
DLP Products
www.dlp.com
www.ti.com/energy
DSP
dsp.ti.com
Industrial
www.ti.com/industrial
www.ti.com/clocks
Medical
www.ti.com/medical
Interface
interface.ti.com
Security
www.ti.com/security
Logic
logic.ti.com
www.ti.com/space-avionics-defense
Power Mgmt
power.ti.com
Transportation and
Automotive
www.ti.com/automotive
Microcontrollers
microcontroller.ti.com
www.ti.com/video
RFID
www.ti-rfid.com
Wireless
www.ti.com/wireless-apps
www.ti.com/lprf
TI E2E Community Home Page
e2e.ti.com
Mailing Address: Texas Instruments, Post Office Box 655303, Dallas, Texas 75265
Copyright 2011, Texas Instruments Incorporated