OsEventSignal(pDrvCtx->hDeviceUnusedEvent); BZERO(eventTable);
eventTable.pfDeviceAttach = DeviceAttach; eventTable.pfDeviceDetach = DeviceDetach; eventTable.pUserData = pDrvCtx;
dwError = WDU_Init(&hDriver, pMatchTables, dwNumMatchTables, &eventTable, sLicense, WD_ACKNOWLEDGE); if (dwError) {
ERR(\failed to initialize USB driver: error 0x%lx \ \dwError, Stat2Str(dwError)); return dwError; }
return WD_STATUS_SUCCESS; }
VOID DriverUninit(DRIVER_CONTEXT *pDrvCtx) {
DEVICE_CONTEXT *pCur, *pTmpDev;
if (pDrvCtx->hEvent)
OsEventClose(pDrvCtx->hEvent); if (pDrvCtx->hMutex)
OsMutexClose(pDrvCtx->hMutex); if (pDrvCtx->hDeviceUnusedEvent)
OsEventClose(pDrvCtx->hDeviceUnusedEvent);
if (hDriver)
WDU_Uninit(hDriver);
/* Release any remaining devices */ pCur = pDrvCtx->deviceContextList; while (pCur) {
pTmpDev = pCur; pCur = pCur->pNext; free(pTmpDev); } }
DWORD OpenUsbDevice( void) {
DWORD dwError; WORD wVendorId = 0; WORD wProductId = 0;
WDU_MATCH_TABLE matchTable;
BZERO(DrvCtx);
wVendorId = USE_DEFAULT; wProductId = USE_DEFAULT;
/* use defaults */
if (wVendorId == USE_DEFAULT) wVendorId = DEFAULT_VENDOR_ID;
if (wProductId == USE_DEFAULT) wProductId = DEFAULT_PRODUCT_ID; BZERO(matchTable);
matchTable.wVendorId = wVendorId; matchTable.wProductId = wProductId;
dwError = DriverInit(&matchTable, 1, DEFAULT_DRIVER_NAME, DEFAULT_LICENSE_STRING, &DrvCtx); if (dwError) {
goto Exit; }
/* Wait for the device to be attached */
dwError = OsEventWait(DrvCtx.hEvent, ATTACH_EVENT_TIMEOUT); if (dwError) {
if (dwError==WD_TIME_OUT_EXPIRED) {
ERR(\expired for connection with the device.\\n\ \that the device is connected and try again.\\n\ } else {
ERR(\OsEventWait() failed on event 0x%p: error 0x%lx \ \DrvCtx.hEvent, dwError, Stat2Str(dwError)); }
goto Exit; }
OsMutexLock(DrvCtx.hMutex); if (!DrvCtx.dwDeviceCount) {
OsMutexUnlock(DrvCtx.hMutex); return 1; }
OsMutexUnlock(DrvCtx.hMutex);
if (!DrvCtx.pActiveDev)
DrvCtx.pActiveDev = DrvCtx.deviceContextList;
OsEventReset(DrvCtx.hDeviceUnusedEvent);
return 0 ; Exit:
DriverUninit(&DrvCtx); return dwError; }
void CloseUsbDevice( void) {
DriverUninit(&DrvCtx); }
DWORD UsbRead(char *pBuffer , DWORD dwBufferSize , PDWORD pdwBytesTransferred) {
DWORD dwError ;
WDU_DEVICE_HANDLE hDevice;
OsMutexLock(DrvCtx.hMutex);
hDevice = DrvCtx.pActiveDev->hDevice; OsMutexUnlock(DrvCtx.hMutex);
dwError = WDU_TransferBulk(hDevice, 0x81,TRUE, 0, pBuffer, dwBufferSize, pdwBytesTransferred, TRANSFER_TIMEOUT);
return dwError ; }
3.驱动程序的发布:
这个也比较简单,请参考自带文档usb manual 的11章节,其实就是用到了他的一个wdreg工具,我写了个批处理文件,想安装的直接点批处理即可!
windriver开发驱动是比较方便,至于稳定性,现在正在测试,看来比较稳定!速度方面500kB是没问题!
不过速度方面pc驱动固然有影响,device的firmware影响也是很大的,特别是双缓冲的ep,处理不当速度很难上去!