PDA

View Full Version : Getting Error 87 with WinUSB_WritePipe with LowPin USB Dev Kit



The Altruist
- 28th December 2013, 01:06
Using PBP3 Gold on Low Pin Count USB Dev Kit (http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en536385) (PIC18F14k50). I'm using the vanilla HID code with minor changes to PID/MFR/Device Name.
I followed the code from here: http://msdn.microsoft.com/en-us/library/windows/hardware/dn376872%28v=vs.85%29.aspx
My PBP main code looks like this: (Nothing exciting, you've all seen this.)


' ************************************************** **********
' * Auto generated EasyHID file. PBP 2.60 and above *
' ************************************************** **********
include "ConfigBits.inc"
' include the HID descriptor
include "DESCUSBProject.bas"

DEFINE OSC 48
DEFINE LOADER_USED 1

USBBufferSizeMax con 32 ' maximum buffer size
USBBufferSizeTX con 32 ' input
USBBufferSizeRX con 32 ' output

' the USB buffer...
USBBuffer Var Byte[USBBufferSizeMax]
USBBufferCount Var Byte

' ************************************************** **********
' * main program loop - remember, you must keep the USB *
' * connection alive with a call to USBService every couple *
' * of milliseconds or so... *
' ************************************************** **********
usbinit ' initialise USB...
ProgramStart:
gosub DoUSBIn
gosub DoUSBOut
goto ProgramStart

' ************************************************** **********
' * receive data from the USB bus *
' ************************************************** **********
DoUSBIn:
USBBufferCount = USBBufferSizeRX ' RX buffer size
USBService ' keep connection alive
USBIn 1, USBBuffer, USBBufferCount, DoUSBIn ' read data, if available
return

' ************************************************** **********
' * wait for USB interface to attach *
' ************************************************** **********
DoUSBOut:
USBBufferCount = USBBufferSizeTX ' TX buffer size
USBService ' keep connection alive
USBOut 1, USBBuffer, USBBufferCount, DoUSBOut ' if bus available, transmit data
return


My WinUSB code looks like this:

#include "pch.h"
#include "USBUtils.h"

LONG __cdecl
_tmain(
LONG Argc,
LPTSTR * Argv
)
/*++

Routine description:

Sample program that communicates with a USB device using WinUSB

--*/
{
DEVICE_DATA deviceData;
HRESULT hr;
USB_DEVICE_DESCRIPTOR deviceDesc;
BOOL bResult;
BOOL noDevice;
ULONG lengthReceived;

UNREFERENCED_PARAMETER(Argc);
UNREFERENCED_PARAMETER(Argv);
//Press any key to continue to allow debugger attaching
system("pause");
//
// Find a device connected to the system that has WinUSB installed using our
// INF
//
hr = OpenDevice(&deviceData, &noDevice);

if (FAILED(hr)) {
if (noDevice) {
printf(_T("Device not connected or driver not installed\n"));
} else {
printf(_T("Failed looking for device, HRESULT 0x%x\n"), hr);
}
return 0;
}

//
// Get device descriptor
//
bResult = WinUsb_GetDescriptor(deviceData.WinusbHandle,
USB_DEVICE_DESCRIPTOR_TYPE,
0,
0,
(PBYTE) &deviceDesc,
sizeof(deviceDesc),
&lengthReceived);

if (FALSE == bResult || lengthReceived != sizeof(deviceDesc)) {
printf(_T("Error among LastError %d or lengthReceived %d\n"),
FALSE == bResult ? GetLastError() : 0,
lengthReceived);
CloseDevice(&deviceData);
return 0;
}

//
// Print a few parts of the device descriptor
//
printf(_T("Device found: VID_%04X&PID_%04X; bcdUsb %04X\n"),
deviceDesc.idVendor,
deviceDesc.idProduct,
deviceDesc.bcdUSB);
//Insert Actual code here.
UCHAR DeviceSpeed;
bResult = GetUSBDeviceSpeed(deviceData.WinusbHandle, &DeviceSpeed);
if (bResult == FALSE){
printf("Something broke in GetUSBDeviceSpeed.\n");
return(EXIT_FAILURE);
}

PIPE_ID pipeUSB;

bResult = QueryDeviceEndpoints(deviceData.WinusbHandle, &pipeUSB);
if (bResult == FALSE){
printf("Something broke in QueryDeviceEndpoints.\n");
return(EXIT_FAILURE);
}
ULONG cbSize;
UCHAR szBuffer[] = "Hello World";
cbSize = (ULONG)strlen((char*)szBuffer);
ULONG cbSent;
printf("Writing to Bulk Endpoint.\n");
bResult = WinUsb_WritePipe(deviceData.WinusbHandle, pipeUSB.PipeOutId, szBuffer, cbSize, &cbSent, 0);
//bResult = WriteToBulkEndpoint(deviceData.WinusbHandle, &pipeUSB.PipeOutId, &cbSize);
if (bResult == FALSE){
printf("Something broke in WriteToBulkEndpoint.\n");
printf(_T("Error among LastError %d\n"),GetLastError());

return(EXIT_FAILURE);
}
else{
printf("Wrote %d bytes to device.\n", cbSent);
}
printf("Reading from Bulk Endpoint.\n");
bResult = ReadFromBulkEndpoint(deviceData.WinusbHandle, &pipeUSB.PipeInId, cbSize);
if (bResult == FALSE){
printf("Something broke in ReadFromBulkEndpoint.\n");
printf(_T("Error among LastError %d\n"), GetLastError());
return(EXIT_FAILURE);
}
cbSize = 256;
system("pause");

//End actual code
CloseDevice(&deviceData);
return EXIT_SUCCESS;
}


My output looks like this:

Device found: VID_1781&PID_07BD; bcdUsb 0110
Device speed: 1 (Low speed).
Endpoint index: 0 Pipe type: Interrupt Pipe ID: 3.
Endpoint index: 1 Pipe type: Interrupt Pipe ID: 3.
Writing to Bulk Endpoint.
Something broke in WriteToBulkEndpoint.
Error among LastError 87


Now, obviously, I'm not writing to a bulk endpoint, I'm writing to an interrupt endpoint. I just not sure how to finagle that.

So how do I fix this?