微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 如何编写Windows CE.net的usb驱动程序(2)

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

时间:05-02 来源:互联网 点击:

t=NULL;

}

if(m_hInterruptTransfer)

(*m_lpUsbFuncs->lpCloseTransfer)(m_hInterruptTransfer);

if(m_hInterruptPipe)

(*m_lpUsbFuncs->lpClosePipe)(m_hInterruptPipe);

}

//初始化USB鼠标驱动程序

BOOLCMouse::Initialize()

{

LPCUSB_DEVICElpDeviceInfo=(*m_lpUsbFuncs->lpGetDeviceInfo)(m_hDevice);

//检测配置:USB鼠标应该只有一个中断管道

if((m_pInterface->lpEndpoints[0].Descriptor.bmAttributesUSB_ENDPOINT_TYPE_MASK)!=USB_ENDPOINT_TYPE_INTERRUPT)

{

RETAILMSG(1,(TEXT(!USBMouse:EP0wrongtype(%u)!rn),

m_pInterface->lpEndpoints[0].Descriptor.bmAttributes));

returnFALSE;

}

DEBUGMSG(ZONE_INIT,(TEXT(USBMouse:EP0:MaxPacket:%u,Interval:%urn),

m_pInterface->lpEndpoints[0].Descriptor.wMaxPacketSize,

m_pInterface->lpEndpoints[0].Descriptor.bInterval));

m_hInterruptPipe=(*m_lpUsbFuncs->lpOpenPipe)(m_hDevice,

m_pInterface->lpEndpoints[0].Descriptor);

if(m_hInterruptPipe==NULL){

RETAILMSG(1,(TEXT(Mouse:Erroropeninginterruptpipern)));

return(FALSE);

}

m_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);

if(m_hEvent==NULL)

{

RETAILMSG(1,(TEXT(USBMouse:ErroronCreateEventforconnecteventrn)));

return(FALSE);

}

//创建数据接受线程

m_hThread=CreateThread(0,0,MouseThreadStub,this,0,NULL);

if(m_hThread==NULL)

{

RETAILMSG(1,(TEXT(USBMouse:ErroronCreateThreadrn)));

return(FALSE);

}

return(TRUE);

}

//从USB鼠标设备中读出数据,产生相应的鼠标事件。

BOOLCMouse::SubmitInterrupt()

{

if(m_hInterruptTransfer)

(*m_lpUsbFuncs->lpCloseTransfer)(m_hInterruptTransfer);

//从USB鼠标PIPE中读数据

m_hInterruptTransfer=(*m_lpUsbFuncs->lpIssueInterruptTransfer)

(m_hInterruptPipe,MouseTransferCompleteStub,this,

USB_IN_TRANSFER|USB_SHORT_TRANSFER_OK,//表示读数据

min(m_pInterface->lpEndpoints[0].Descriptor.wMaxPacketSize,

sizeof(m_pbDataBuffer)),

m_pbDataBuffer,

NULL);

if(m_hInterruptTransfer==NULL)

{

DEBUGMSG(ZONE_ERROR,(L!USBMouse:ErrorinIssueInterruptTransferrn));

returnFALSE;

}

else

{

DEBUGMSG(ZONE_TRANSFER,(LUSBMouse::SubmitInterrupt,Transfer:0x%Xrn,

m_hInterruptTransfer));

}

returnTRUE;

}

//处理鼠标中断传输的数据

BOOLCMouse::HandleInterrupt()

{

DWORDdwError;

DWORDdwBytes;

DWORDdwFlags=0;

INTdx=(signedchar)m_pbDataBuffer[1];

INTdy=(signedchar)m_pbDataBuffer[2];

BOOLfButton1=m_pbDataBuffer[0]0x01?TRUE:FALSE;

BOOLfButton2=m_pbDataBuffer[0]0x02?TRUE:FALSE;

BOOLfButton3=m_pbDataBuffer[0]0x04?TRUE:FALSE;

if(!(*m_lpUsbFuncs->lpGetTransferStatus)(m_hInterruptTransfer,dwBytes,dwError))

{

DEBUGMSG(ZONE_ERROR,(TEXT(!USBMouse:ErrorinGetTransferStatus(0x%X)rn),

m_hInterruptTransfer));

returnFALSE;

}

else

{

DEBUGMSG(ZONE_TRANSFER,(TEXT(USBMouse::HandleInterrupt,hTransfer0x%Xcomplete(%ubytes,Error:%X)rn),

m_hInterruptTransfer,dwBytes,dwError));

}

if(!SubmitInterrupt())

returnFALSE;

if(dwError!=USB_NO_ERROR)

{

DEBUGMSG(ZONE_ERROR,(TEXT(!USBMouse:Error0x%Xininterrupttransferrn),dwError));

returnTRUE;

}

if(dwBytes3)

{

DEBUGMSG(ZONE_ERROR,(TEXT(!USBMouse:Invalidbytecnt%ufrominterrupttransferrn),dwBytes));

returnTRUE;

}

if(dx||dy)

dwFlags|=MOUSEEVENTF_MOVE;

if(fButton1!=m_fPrevButton1)

{

if(fButton1)

dwFlags|=MOUSEEVENTF_LEFTDOWN;

else

dwFlags|=MOUSEEVENTF_LEFTUP;

}

if(fButton2!=m_fPrevButton2)

{

if(fButton2)

dwFlags|=MOUSEEVENTF_RIGHTDOWN;

else

dwFlags|=MOUSEEVENTF_RIGHTUP;

}

if(fButton3!=m_fPrevButton3)

{

if(fButton3)

dwFlags|=MOUSEEVENTF_MIDDLEDOWN;

else

dwFlags|=MOUSEEVENTF_MIDDLEUP;

}

m_fPrevButton1=fButton1;

m_fPrevButton2=fButton2;

m_fPrevButton3=fButton3;

DEBUGMSG(ZONE_EVENTS,

(TEXT(USBMouseevent:dx:%d,dy:%d,dwFlags:0x%X(B1:%u,B2:%u,B3:%u)rn),

dx,dy,dwFlags,fButton1,fButton2,fButton3));

//通知系统产生鼠标事件

if(m_fReadyForMouseEvents)

mouse_event(dwFlags,dx,dy,0,0);

else

m_fReadyForMouseEvents=IsAPIReady(SH_WMGR);

returnTRUE;

}

DWORDCALLBACKCMouse::MouseTransferCompleteStub(LPVOIDlpvNotifyParameter)

{

CMouse*p

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top