beautypg.com

E.4.2 establishing a connection to the device, E.4.2, Establishing a connection to the device – Motorola USB08 User Manual

Page 144: Universal usb device driver (usbio)

background image

Designer Reference Manual

USB08 Evaluation Board

144

Universal USB Device Driver (USBIO)

MOTOROLA

Universal USB Device Driver (USBIO)

Handle2 and Handle3 are called pipe handles. Note that while Handle1
cannot be used to communicate with a pipe, any operation on the device
can be executed by using Handle2 or Handle3, too.

E.4.2 Establishing a Connection to the Device

The following code sample demonstrates the steps that are necessary
at the USBIO API to establish a handle for a device and a pipe. The code
is not complete, no error handling is included.

// include the interface header file of USBIO.SYS

#include “usbio_i.h”

// device instance number

#define DEVICE_NUMBER

0

// some local variables

HANDLE FileHandle;

USBIO_SET_CONFIGURATION SetConfiguration;

USBIO_BIND_PIPE BindPipe;
HDEVINFO DevInfo;

GUID g_UsbioID = USBIO_IID;

SP_DEVICE_INTERFACE_DATA DevData;

SP_INTERFACE_DEVICE_DETAIL_DATA *DevDetail = NULL;
DWORD ReqLen;

DWORD BytesReturned;

// enumerate the devices
// get a handle to the device list

DevInfo = SetupDiGetClassDevs (&g_UsbioID,

NULL,NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT);

// get the device with index DEVICE_NUMBER
SetupDiEnumDeviceInterfaces (DevInfo, NULL,

&g_usbioID, DEVICE_NUMBER, &DevData );

// get length of detailed information

SetupDiGetDeviceInterfaceDetail (DevInfo, &DevData, NULL,

0, &ReqLen, NULL);

// allocate a buffer

DevDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc (ReqLen);

// now get the detailed device information
DevDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

SetupDiGetDeviceInterfaceDetail (DevInfo, &DevData, DevDetail,

ReqLen, &ReqLen, NULL);

// open the device, use OVERLAPPED flag if necessary
// use DevDetail->DevicePath as device name

FileHandle = CreateFile(

DevDetail->DevicePath,

GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,

NULL,

OPEN_EXISTING,

0 /* or FILE_FLAG_OVERLAPPED */,
NULL);