From: <lar...@bl...> - 2006-11-21 15:16:32
|
Hello, I did not know where to post and I think that this can be helpful for the people who work with Windows CE 5.0. Hopefully this e-mail will appear in the search engines results The goal is simple - send (and receive) raw data from Windows CE 5.0 device to the PC Now problems. The device (WinCE) does not have FLASH based file system. ActiveSync is not an option, because of limited resources in the embedded device and lack of portability of the interface across different host operating systems. I failed to initialize storage class device in Win CE 5.0 This is apparently a new feature for Win CE. Before CE 5.0 MS truly believed that ActiveSync is all a man will ever need. In Win CE 5.0 Storage Class is officially supported, but still requires so called file system driver in case of system with only one FLASH device (here i may be wrong). Long story short - storage class did not work for me. I chose serial class You need three things in WinCE - add USB function serial class to the components tree in the Platform Builder - add USB driver (easy in my case, because BSPs for Intel Xscale ARM already have USB driver) - add to the file platform.reg lines ; ----------------------------------------------------------------------------- ; @CESYSGEN IF CE_MODULES_USBFN IF BSP_NOUSBFN ! [HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers] "DefaultClientDriver"=- ; erase previous default "ClientDriver"=- [HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers] "DefaultClientDriver"="Serial_Class" "ClientDriver"="\\Drivers\\USB\\FunctionDrivers\\Serial_Class" [HKEY_LOCAL_MACHINE\Drivers\BuiltIn\Usbfn] "Prefix"="UFN" "Dll"="gum_usbfn.dll" "Order"=dword:0 "Irq"=dword:b ; 11 IRQ_USB "MemBase"=dword:40600000 ; UDC Register "MemLen"=dword:1000 ; Needs at least 0xE04. Round up to 1 page. "BusIoctl"=dword:2a0048 "InterfaceType"=dword:0 ; Internal "Priority256"=dword:64 "IClass"=multi_sz:"{E2BDC372-598F-4619-BC50-54B3F7848D35}=%b","{6F40791D-300E-44E4-BC38-E0E63CA8375C}=%b" [HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers\Serial_Class] "Dll"="serialusbfn.dll" ; "DeviceName"="USBFNS1:" "Index"=dword:6 ; COM6 "Prefix"="COM" ; "DeviceArrayIndex"=dword:0 ; Use to load different USB function serial client driver objects. "IClass"="{CC5195AC-BA49-48a0-BE17-DF6D1B0173DD}" "idVendor"=dword:23E2 "Manufacturer"="NICOM" "idProduct"=dword:2152 "Product"="NICOM device" ; "bcdDevice"=dword:0 ; "Tsp"="Unimodem.dll" ; "DeviceType"=dword:0 [HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers\Serial_Class\Config] "EnableFlowHard"=dword:0 "EnableFlowSoft"=dword:0 [HKEY_LOCAL_MACHINE\Comm\DefaultConnections] "USB"="USB Cable" [HKEY_CURRENT_USER\ControlPanel\Comm] "Cnct"="USB" [HKEY_CURRENT_USER\Comm\RasBook\USB] "Entry"=hex:\ 08,02,40,00,00,00,00,00,00,00,00,00,00,00,04,12,70,19,0e,00,01,00,00,00,00,\ 1d,39,03,00,00,00,00,00,00,00,00,95,00,00,00,f0,7b,39,03,f8,00,00,00,a8,e9,\ 04,12,df,00,00,00,00,00,00,00,00,00,00,00,10,1d,39,03,98,00,00,00,f7,00,00,\ 00,df,00,00,00,f8,00,00,00,f8,00,00,00,84,ea,04,12,b8,33,39,03,98,00,00,00,\ f7,00,00,00,df,00,00,00,f8,00,00,00,00,00,00,00,95,00,00,00,f0,7b,39,03,0c,\ 01,00,00,f8,e9,04,12,e1,00,00,00,00,00,00,00,00,00,00,00,70,19,0e,00,96,00,\ 00,00,00,1d,39,03,c0,19,0e,00,00,00,00,00,00,00,00,00,aa,aa,aa,aa,00,00,00,\ 00,00,00,00,00,f0,00,00,00,df,fd,ff,ff,0b,00,07,0b,46,00,00,00,01,00,00,00,\ c0,fb,ff,ff,00,00,00,00,ff,00,00,00,00,ff,00,00,00,00,ff,00,00,00,00,ff,01,\ 00,00,00,00,00,ff,00,00,1d,39,03,c0,19,0e,00,01,00,00,00,34,f0,04,12,6c,68,\ 13,0a,00,00,00,00,00,00,00,00,70,19,0e,00,04,00,00,00,00,00,00,00,f0,38,39,\ 03,e1,00,00,00,0c,01,00,00,01,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,64,00,69,00,72,\ 00,65,00,63,00,74,00,00,00,00,00,a8,58,01,00,ca,27,05,12,ca,27,05,12,09,00,\ 00,00,fc,d6,55,00,53,00,42,00,20,00,43,00,61,00,62,00,6c,00,65,00,00,00,08,\ 00,ca,27,05,12,90,ea,04,12,00,00,00,00,00,00,00,00,34,f0,04,12,00,00,00,00,\ 70,68,13,0a,f1,00,00,00,00,00,00,00,f0,f0,00,00,00,00,00,00,00,00,00,00,01,\ 00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,\ ff,00,00,1d,39,03,c0,19,0e,00,aa,29,e5,23,2c,eb,04,12,24,eb,04,12,0c,e8,f7,\ 03,98,b1,f8,03,a0,2c,08,00,89,01,00,00,00,00,00,00,ca,27,05,12,00,00,00,00,\ 00,00,00,00,ac,04,02,00,de,00,00,00,09,01,00,00,08,01,00,00,00,00,00,00,ca,\ 27,05,12,00,00,00,00,aa,29,e5,23,00,00,00,00,a0,2c,08,00,00,00,00,00,40,72,\ 01,7c,90,09,00,00,00,00,00,00,09,00,00,00,80,10,00,00,89,01,00,00,40,72,01,\ 7c,02,00,00,00,c0,4b,01,7c,00,00,00,00,ca,27,05,12,00,00,00,00,c0,4b,01,7c,\ 01,00,00,00,70,c0,01,00,89,01,00,00,00,00,01,7c,89,01,00,00,f8,c0,01,00,ca,\ 27,05,12,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,\ 00,00,00,00,00,00,f8,49,f8,03,18,2e,05,00,c0,19,0e,00,ff,00,00,00,ec,62,01,\ 00,c0,6c,01,7c,00,00,00,00,02,00,00,00,89,01,00,00,40,72,01,7c,02,00,00,00,\ 00,00,00,00,00,00,00,00,e4,58,01,00,09,00,00,00,80,c3,01,00,00,00,00,00,00,\ 00,00,00,98,00,00,00,f8,00,00,00,df,00,00,00,0a,01,00,00,ca,27,05,12,81,00,\ 00,00,50,dc,13,0a,1c,ec,04,12,20,b7,07,00,40,72,01,7c,89,01,00,00,02,00,00,\ 00,ca,27,05,12,50,dc,13,0a,0d,00,00,00,81,00,00,00,ca,27,05,12,ca,27,05,12,\ 60,71,01,7c,c0,c4,07,00,f8,00,00,00,22,ff,ff,ff,0a,01,00,00,69,ff,ff,ff,22,\ ff,ff,ff,f7,fe,ff,ff,69,ff,ff,ff,09,ff,ff,ff,f7,fe,ff,ff,22,ff,ff,ff,09,ff,\ ff,ff,69,ff,ff,ff,c0,19,0e,00,1c,10,39,03,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,ce,c4,cd,63,0d,00,00,00,ca,27,05,12,0c,e8,f7,03,d8,a1,f8,03,\ 00,00,00,00,0d,00,00,00,20,ff,01,00,e0,ec,04,12,e4,ec,04,12,0d,00,00,00,dc,\ c3,07,00,0d,00,00,00,00,00,00,00,ca,27,05,12,00,00,00,00,00,00,00,00,ac,04,\ 02,00,e4,ec,04,12,dc,ec,04,12,2c,11,13,00,00,00,00,00,ca,27,05,12,00,00,00,\ 00,aa,29,e5,23,00,00,00,00,dc,c3,07,00,00,00,00,00,60,71,01,7c,ca,27,05,12,\ 0a,00,00,00,09,00,00,00,00,c8,ff,ff,0d,00,00,00,60,71,01,7c,81,00,00,00,c0,\ 4b,01,7c,00,00,00,00,00,00,00,00,00,00,00,12,c8,af,f6,83,0f,00,00,00,00,00,\ 00,00,00,e0,c8,83,70,c5,cf,80,98,07,cd,83,c8,af,f6,83,0f,00,00,00,38,fe,00,\ f0,88,ed,04,12,9c,c0,10,80,0f,00,00,00,00,00,00,00,c4,b3,00,f0,64,f0,04,12,\ 4e,00,00,00,00,00,00,00,00,00,00,00,c0,6c,01,7c,54,ef,04,12,e0,94,10,80,f1,\ ec,ff,ff,00,00,00,00,00,00,00,00 ENDIF BSP_NOUSBFN ! ; @CESYSGEN ENDIF CE_MODULES_USBFN ; ----------------------------------------------------------------------------- Compile WinCE application with the following code // USBTest.cpp : Defines the entry point for the application. // #include "stdafx.h" #include <windows.h> #include <types.h> HANDLE serialPort; DWORD eventMask; const char USBTxString[] = "NICOM's USB ... and Miasnikov forever!"; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { TCHAR portName[10]; DCB portDCB; COMMTIMEOUTS portTimeouts; UINT8 rxData[64]; UINT8 txData[64]; DWORD readBytes, writtenBytes; BOOL status; _tprintf(_T("USB Test Application is running \r\n")); do { wcscpy(portName, _T("COM6:")); serialPort = CreateFile( portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL ); if (serialPort == INVALID_HANDLE_VALUE) { _tprintf(_T("Serial Port %s cannot be opened, error = %d \r\n"), portName, GetLastError()); break; } status = GetCommState(serialPort, &portDCB); if (status == 0) { _tprintf(_T("COM State Get Failure, error = %d \r\n"), GetLastError()); break; } #if 1 portDCB.BaudRate = CBR_9600; #else portDCB.BaudRate = CBR_256000; #endif portDCB.Parity = NOPARITY; portDCB.ByteSize = 8; portDCB.StopBits = ONESTOPBIT; status = SetCommState(serialPort, &portDCB); if (status == 0) { _tprintf(_T("COM State Set Failure, error = %d \r\n"), GetLastError()); break; } #if 0 txData = 0x55; writtenBytes = 1; _tprintf(_T("Writing data to COM ... \r\n")); while (status = WriteFile(serialPort, &txData, 1, &writtenBytes, NULL)) { _tprintf(_T("OK. ")); if (writtenBytes) _tprintf(_T("Tx Data = %d \r\n"), txData); else break; // no bytes has been written Sleep(1000); // transmit data once per second } if (status == 0) { _tprintf(_T("COM Data Write Failure, error = %d \r\n"), GetLastError()); break; } #endif #if 1 status = GetCommTimeouts(serialPort, &portTimeouts); if (status == 0) { _tprintf(_T("COM Timeouts Get Failure, error = %d \r\n"), GetLastError()); break; } portTimeouts.ReadIntervalTimeout = MAXDWORD; portTimeouts.ReadTotalTimeoutMultiplier = 0; portTimeouts.ReadTotalTimeoutConstant = 0; status = SetCommTimeouts(serialPort, &portTimeouts); if (status == 0) { _tprintf(_T("COM Timeouts Get Failure, error = %d \r\n"), GetLastError()); break; } status = SetCommMask(serialPort, EV_BREAK | EV_RXCHAR); if (status == 0) { _tprintf(_T("COM Event Mask Set Failure, error = %d \r\n"), GetLastError()); break; } while (status = WaitCommEvent(serialPort, &eventMask, NULL)) { status = SetCommMask(serialPort, EV_BREAK | EV_RXCHAR); if (status == 0) { _tprintf(_T("COM Event Mask Set Failure, error = %d \r\n"), GetLastError()); status = 1; /* set back a valid status of WaitCommEvent for following handling */ break; } if (eventMask & EV_RXCHAR) { while (status = ReadFile(serialPort, &rxData, 64, &readBytes, NULL)) { if (readBytes) { _tprintf(_T("Rx Data =")); for (UINT8 index = 0; index < readBytes; index++) _tprintf(_T(" %d"), rxData[index]); _tprintf(_T("\r\n \r\n")); } else break; // end of file - no more bytes to read } if (status == 0) { _tprintf(_T("COM Data Read Failure, error = %d \r\n"), GetLastError()); status = 1; /* set back a valid status of WaitCommEvent for following handling */ break; } } #if 1 break; #endif if (eventMask & EV_BREAK) break; } if (status == 0) { _tprintf(_T("COM Event Wait Failure, error = %d \r\n"), GetLastError()); break; } #endif #if 1 _tprintf(_T("Writing data to COM ... \r\n")); memcpy(txData, USBTxString, strlen(USBTxString)); while (status = WriteFile(serialPort, txData, strlen(USBTxString), &writtenBytes, NULL)) { _tprintf(_T("Tx Bytes = %d \r\n"), writtenBytes); if (writtenBytes) { _tprintf(_T("Tx Data =")); for (UINT8 index = 0; index < writtenBytes; index++) _tprintf(_T(" %c"), txData[index]); _tprintf(_T("\r\n \r\n")); } else break; // no bytes has been written Sleep(1000); // transmit data once per second } if (status == 0) { _tprintf(_T("COM Data Write Failure, error = %d \r\n"), GetLastError()); break; } #endif } while(0); _tprintf(_T("Exiting from the USB Test Application \r\n")); return 0; } Ok, now Windows host side (Win32) You need libUSB-win32 standard installation. Install it. Compile file #include <usb.h> #include <stdio.h> #include <string.h> #if 1 /* the device's vendor and product id */ #define MY_VID 0x23E2 #define MY_PID 0x2152 #endif #if 0 /* the device's vendor and product id */ #define MY_VID 0x067B #define MY_PID 0x2303 #endif /* the device's endpoints */ #define EP_IN 0x81 #define EP_OUT 0x02 #define BUF_SIZE 16 #if 1 const char USBTxData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; #endif usb_dev_handle *open_dev(void); usb_dev_handle *open_dev(void) { struct usb_bus *bus; struct usb_device *dev; printf("Scan USB bus\n"); for(bus = usb_get_busses(); bus; bus = bus->next) { printf("Bus = %d from %d\n", bus, usb_get_busses()); for(dev = bus->devices; dev; dev = dev->next) { if(dev->descriptor.idVendor == MY_VID && dev->descriptor.idProduct == MY_PID) { return usb_open(dev); } else { printf("vid=%08X did=%08X\n", dev->descriptor.idVendor, dev->descriptor.idProduct); } } } return NULL; } int main(void) { usb_dev_handle *dev = NULL; /* the device handle */ char tmp[BUF_SIZE * 4]; int index, size; usb_init(); /* initialize the library */ usb_find_busses(); /* find all busses */ usb_find_devices(); /* find all connected devices */ if(!(dev = open_dev())) { printf("error: device not found!\n"); return 0; } if(usb_set_configuration(dev, 1) < 0) { printf("error: setting config 1 failed\n"); usb_close(dev); return 0; } if(usb_claim_interface(dev, 0) < 0) { printf("error: claiming interface 0 failed\n"); usb_close(dev); return 0; } #if 0 if(usb_bulk_write(dev, EP_OUT, tmp, sizeof(tmp), 5000) != sizeof(tmp)) { printf("error: bulk write failed\n"); } #endif printf("Writing to the device\n"); #if 1 memcpy(tmp, USBTxData, sizeof(USBTxData)); #endif do { size = usb_bulk_write(dev, EP_OUT, tmp, sizeof(USBTxData), 5000); if(size <= 0) { printf("error: bulk write failed %d\n", size); } else { printf("Sent: %d bytes\n", size); } } while (0); #if 1 printf("Reading from the device\n"); while (1) { #if 1 memset(tmp, 'X', sizeof(tmp)); #endif #if 1 size = usb_bulk_read(dev, EP_IN, tmp, sizeof(tmp), 5000); if(size <= 0) { printf("error: bulk read failed %d\n", size); } else #endif { printf("Got: %d bytes\n", size); // tmp[sizeof(tmp) - 1] = 0; tmp[size - 1] = 0; printf("Read: %s \n\n", tmp); // printf("RxData:"); // for (index = 0; index < size; index++) printf(" %d", tmp[index]); // printf("\n\n"); } } #endif usb_release_interface(dev, 0); usb_close(dev); return 0; } |