新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 如何编写Windows CE.net的usb驱动程序(2)

如何编写Windows CE.net的usb驱动程序(2)

作者:时间:2012-05-02来源:网络收藏

上述讲了堆理论,可能读者脑袋都已经大了,为此,我们举个简单的例子来详细说明一下的开发过程。

本文引用地址:http://www.eepw.com.cn/article/149162.htm

例如我们有个USBMouse设备,设备信息描述如下:

DeviceDescriptor:

bcdUSB:0x0100

bDeviceClass:0x00

bDeviceSubClass:0x00

bDeviceProtocol:0x00

bMaxPacketSize0:0x08(8)

idVendor:0x05E3(GenesysLogicInc.)

idProduct:0x0001

bcdDevice:0x0101

iManufacturer:0x00

iProduct:0x01

iSerialNumber:0x00

bNumConfigurations:0x01

ConnectionStatus:DeviceConnected

CurrentConfigValue:0x01

DeviceBusSpeed:Low

DeviceAddress:0x02

OpenPipes:1

EndpointDescriptor:

bEndpointAddress:0x81

TransferType:Interrupt

wMaxPacketSize:0x0003(3)

bInterval:0x0A

可以看出上述设备有一个中断PIPE,包的最大值为3。可能有人问上述的值怎么得到的,win2k的DDK中有个view的例程,编译一下,将你的USB设备插到PC机的USB口中,运行view.exe即可看得相应的设备信息。

有了这些基本信息,就可以USB设备了,首先声明一下,下面的代码取自微软的USB鼠标样本程序,版权归微软所有,此处仅仅借用来描述一下USB鼠标驱动的开发过程,读者如需要引用此代码,需要得到微软的同意。

首先,必须输出USBD要求调用的三个函数,首先到设备插入到USB端口时,USBD会调用USBDeviceAttach()函数,相应的代码如下:

externCBOOL

USBDeviceAttach(

USB_HANDLEhDevice,//USB设备句柄

LPCUSB_FUNCSlpUsbFuncs,//USBDI的函数集合

LPCUSB_INTERFACElpInterface,//设备接口描述信息

LPCWSTRszUniqueDriverId,//设备ID描述字符串。

LPBOOLfAcceptControl,//返回TRUE,标识我们可以控制此设备,反之表示不能控制

DWORDdwUnused)

{

*fAcceptControl=FALSE;

//我们的鼠标设备有特定的描述信息,要检测是否是我们的设备。

if(lpInterface==NULL)

returnFALSE;

//打印相关的USB设备接口描述信息。

DEBUGMSG(ZONE_INIT,(TEXT(USBMouse:DeviceAttach,IF%u,#EP:%u,Class:%u,Sub:%u,Prot:%urn),lpInterface->Descriptor.bInterfaceNumber,lpInterface->Descriptor.bNumEndpoints,lpInterface->Descriptor.bInterfaceClass,lpInterface->Descriptor.bInterfaceSubClass,lpInterface->Descriptor.bInterfaceProtocol));

//初试数据USB鼠标类,产生一个接受USB鼠标数据的线程

CMouse*pMouse=newCMouse(hDevice,lpUsbFuncs,lpInterface);

if(pMouse==NULL)

returnFALSE;

if(!pMouse->Initialize())

{

deletepMouse;

returnFALSE;

}

//注册一个监控USB设备事件的回调函数,用于监控USB设备是否已经拔掉。

(*lpUsbFuncs->lpRegisterNotificationRoutine)(hDevice,

USBDeviceNotifications,pMouse);

*fAcceptControl=TRUE;

returnTRUE;

}

第二个函数是USBInstallDriver()函数,

一些基本定义如下:

constWCHARgcszRegisterClientDriverId[]=LRegisterClientDriverID;

constWCHARgcszRegisterClientSettings[]=LRegisterClientSettings;

constWCHARgcszUnRegisterClientDriverId[]=LUnRegisterClientDriverID;

constWCHARgcszUnRegisterClientSettings[]=LUnRegisterClientSettings;

constWCHARgcszMouseDriverId[]=LGeneric_Sample_Mouse_Driver;

函数接口如下:

externCBOOL

USBInstallDriver(

LPCWSTRszDriverLibFile)//@parm[IN]-ContainsclientdriverDLLname

{

BOOLfRet=FALSE;

HINSTANCEhInst=LoadLibrary(LUSBD.DLL);

//注册USB设备信息

if(hInst)

{

LPREGISTER_CLIENT_DRIVER_IDpRegisterId=(LPREGISTER_CLIENT_DRIVER_ID)

GetProcAddress(hInst,gcszRegisterClientDriverId);

LPREGISTER_CLIENT_SETTINGSpRegisterSettings=

(LPREGISTER_CLIENT_SETTINGS)GetProcAddress(hInst,

gcszRegisterClientSettings);

if(pRegisterIdpRegisterSettings)

{

USB_DRIVER_SETTINGSDriverSettings;

DriverSettings.dwCount=sizeof(DriverSettings);

//设置我们的特定的信息。

DriverSettings.dwVendorId=USB_NO_INFO;

DriverSettings.dwProductId=USB_NO_INFO;

DriverSettings.dwReleaseNumber=USB_NO_INFO;

DriverSettings.dwDeviceClass=USB_NO_INFO;

DriverSettings.dwDeviceSubClass=USB_NO_INFO;

DriverSettings.dwDeviceProtocol=USB_NO_INFO;

DriverSettings.dwInterfaceClass=0x03;//HID

DriverSettings.dwInterfaceSubClass=0x01;//bootdevice

DriverSettings.dwInterfaceProtocol=0x02;//mouse

fRet=(*pRegisterId)(gcszMouseDriverId);

if(fRet)

{

fRet=(*pRegisterSettings)(szDriverLibFile,

gcszMouseDriverId,NULL,DriverSettings);

if(!fRet)

{

//BUGBUGunregistertheClientDriver’sID

}

}

}

else

{

RETAILMSG(1,(TEXT(!USBMouse:ErrorgettingUSBDfunctionpointersrn)));

}

FreeLibrary(hInst);

}

returnfRet;

}

上述代码主要用于产生USB设备需要的注册表信息,需要注意的是:USB设备不使用标准的注册表函数,而是使用RegisterClientDriverID()和RegisterClientSettings来注册相应的设备信息。


上一页 1 2 3 4 下一页

评论


相关推荐

技术专区

关闭