SLAA453A january   2011  – may 2023

 

  1.   1
  2. 1MSP430™ USB HID Windows API Programmer's Guide
  3. 2Introduction
  4. 3Implementation
    1. 3.1 Overview
    2. 3.2 File Organization
    3. 3.3 System Requirements
    4. 3.4 MSP430 USB API Stacks
    5. 3.5 How Windows Maps Physical USB HID Devices to the Host Application
    6. 3.6 Locating a Specific HID Device/Interface on the System and Opening It
    7. 3.7 Sending/Receiving Data
    8. 3.8 Detecting the Dynamic Connection/Disconnection of HID Devices
  5. 4Function Call Reference
    1. 4.1 Device Connection Management and Initialization Calls
      1. 4.1.1 VOID HID_Init(struct strHidDevice* pstrHidDevice)
      2. 4.1.2 DWORD HID_GetSerNums(WORD vid, WORD pid, struct strTrackSerialNumbers *serialNumList)
      3. 4.1.3 DWORD HID_GetNumOfInterfaces(WORD vid, WORD pid, DWORD numSerNums)
      4. 4.1.4 BYTE HID_Open(struct strHidDevice* pstrHidDevice, WORD vid, WORD pid, DWORD deviceIndex, char serialNumber[SERNUM_LEN], DWORD totalDevNum, DWORD totalSerNum)
      5. 4.1.5 BYTE HID_Close(struct strHidDevice* pstrHidDevice)
      6. 4.1.6 BYTE HID_GetVersionNumber(struct strHidDevice* pstrHidDevice, USHORT * VersionNumber)
    2. 4.2 Sending/Receiving Data
      1. 4.2.1 BYTE HID_WriteFile(struct strHidDevice* pstrHidDevice, BYTE* buffer, DWORD bufferSize, DWORD* bytesSent)
      2. 4.2.2 BYTE HID_ReadFile(struct strHidDevice* pStrHidDevice, BYTE* buffer, DWORD bufferSize, DWORD* bytesReturned)
    3. 4.3 Plug and Play Management
      1. 4.3.1 BYTE HID_RegisterForDeviceNotification(HWND hWnd, HDEVNOTIFY* diNotifyHandle)
      2. 4.3.2 BYTE HID_UnregisterForDeviceNotification(HDEVNOTIFY* diNotifyHandle)
      3. 4.3.3 BOOL IsDeviceAffected(struct strHidDevice* pstrHidDevice)
  6. 5Demo Application
  7. 6MSP430 USB Tool Suite
  8.   HID Interface Data Structure: strHidDevice
  9.   Format of Reports on HID-Datapipe Devices
  10.   C References
  11.   C Revision History

Detecting the Dynamic Connection/Disconnection of HID Devices

Unlike with traditional COM ports, USB applications must be aware that the user may detach (or attach) a device at any time. This is the dynamic "plug-and-play" aspect of USB. Windows handles this through notifications, implemented in the MFC library. This occurs outside the API, because the notifications go directly to the application's window object; but when these notifications are received, the application can call API handlers specifically provided for this purpose. The demo application shows how this can be done.

These are the steps required for an application to handle these notifications:

  1. At the beginning of the application, the application should register its window to receive device change notifications by using HID_RegisterForDeviceNotification().
  2. Put ON_WM_DEVICECHANGE into the message map for the window. (Make sure this is outside the AFX_MSG_MAP-commented section.)
  3. Write a function OnDeviceChange() as a public function for the window. This message handler will be called by the MFC framework in response to an ON_WM_DEVICECHANGE notification.

The definition of the function should be as shown:

afx_msg BOOL OnDeviceChange( 
   UINT nEventType, 
   DWORD_PTR dwData  
);

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().