2

I am trying to use the found-bits project to communicate with my STM32F103 via USB from a Windows application with no success.

The program keeps returning an error code 31 at the WinUsb_ControlTransfer function call.

I made some changes that solved the problem for receiving the data from the default end point but the issue remains when trying to send data to it.

How can I get my windows application to send and receive data from the STM32?

P.S.: I created .inf and .cat files for my device and installed the driver

BOOL SendDatatoDefaultEndpoint(WINUSB_INTERFACE_HANDLE hDeviceHandle, BYTE byWinUSBCommControl, BYTE *pbyData = NULL, WORD wNumBytesCount = 0)
{
  if (hDeviceHandle==INVALID_HANDLE_VALUE)
  {
    return FALSE;
  }
  BOOL bResult = TRUE;
  WINUSB_SETUP_PACKET SetupPacket;
  ZeroMemory(&SetupPacket, sizeof(WINUSB_SETUP_PACKET));
  ULONG cbSent = 0;

  //Create the setup packet
  SetupPacket.RequestType = (BMREQUEST_HOST_TO_DEVICE << 7)/* | (BMREQUEST_VENDOR << 5) | BMREQUEST_TO_INTERFACE*/;
  //SetupPacket.Request = byWinUSBCommControl;
  //SetupPacket.Value = 0;
  SetupPacket.Index = 0; // specify WinUSBComm interface
  //SetupPacket.Length = wNumBytesCount;
  SetupPacket.Request = USB_REQUEST_GET_DESCRIPTOR;
  SetupPacket.Value = USB_DEVICE_DESCRIPTOR_TYPE << 8;
  SetupPacket.Length = sizeof(USB_DEVICE_DESCRIPTOR);

  bResult = WinUsb_ControlTransfer(hDeviceHandle, SetupPacket, pbyData, wNumBytesCount, &cbSent, 0);
  if(!bResult)
  {
    printf("Error WinUsb_ControlTransfer: %d.\n", GetLastError());
    goto done;
  }

  PTRACE("Data sent: %d \nActual data transferred: %d.\n", wNumBytesCount, cbSent);

  done:
    return bResult;
}

BOOL GetDataFromDefaultEndpoint(WINUSB_INTERFACE_HANDLE hDeviceHandle, BYTE byWinUSBCommControl, BYTE *pbyData, WORD wNumBytesCount)
{
  if ( 0 == wNumBytesCount )
  {
    return TRUE;
  }

  if ( NULL == pbyData )
  {
    return FALSE;
  }

  if (hDeviceHandle==INVALID_HANDLE_VALUE)
  {
    return FALSE;
  }
  BOOL bResult = TRUE;
  WINUSB_SETUP_PACKET SetupPacket;
  ZeroMemory(&SetupPacket, sizeof(WINUSB_SETUP_PACKET));
  ULONG cbSent = 0;

  //Create the setup packet
  SetupPacket.RequestType = (BMREQUEST_DEVICE_TO_HOST << 7)/* | (BMREQUEST_VENDOR << 5) | BMREQUEST_TO_DEVICE*/;
  //SetupPacket.Request = byWinUSBCommControl;
  //SetupPacket.Value = 0;
  SetupPacket.Index = 0; // specify WinUSBComm interface
  //SetupPacket.Length = wNumBytesCount;
  SetupPacket.Request = USB_REQUEST_GET_DESCRIPTOR;
  SetupPacket.Value = USB_DEVICE_DESCRIPTOR_TYPE << 8;
  SetupPacket.Length = sizeof(USB_DEVICE_DESCRIPTOR);

  bResult = WinUsb_ControlTransfer(hDeviceHandle, SetupPacket, pbyData, wNumBytesCount, &cbSent, 0);
  if(!bResult)
  {
    printf("Error WinUsb_ControlTransfer: %d.\n", GetLastError());
    goto done;
  }

  PTRACE("Data get : %d \nActual data transferred: %d.\n", wNumBytesCount, cbSent);

  done:
    return bResult;
}
Étienne
  • 4,773
  • 2
  • 33
  • 58
rxjsisfine
  • 445
  • 1
  • 10
  • 22
  • When you get a windows error code you can get the description here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx 31 is "A device attached to the system is not functioning." Seems like the problem is on the STM32. Can you check the USB transaction with a USB sniffer? – Étienne May 19 '14 at 19:05

0 Answers0